rcfile.c 34.2 KB
Newer Older
1
/**************************************************************************
2
 *   rcfile.c  --  This file is part of GNU nano.                         *
3
 *                                                                        *
4
5
 *   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,  *
 *   2010, 2011, 2013, 2014 Free Software Foundation, Inc.                *
6
7
8
 *   Copyright (C) 2014 Mike Frysinger                                    *
 *   Copyright (C) 2014, 2015, 2016 Benno Schulenberg                     *
 *                                                                        *
9
10
11
12
 *   GNU nano is free software: you can redistribute it and/or modify     *
 *   it under the terms of the GNU General Public License as published    *
 *   by the Free Software Foundation, either version 3 of the License,    *
 *   or (at your option) any later version.                               *
13
 *                                                                        *
14
15
16
17
 *   GNU nano is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty          *
 *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              *
 *   See the GNU General Public License for more details.                 *
18
19
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
20
 *   along with this program.  If not, see http://www.gnu.org/licenses/.  *
21
22
23
 *                                                                        *
 **************************************************************************/

24
#include "proto.h"
25

26
#include <glob.h>
27
#include <stdarg.h>
28
29
30
#include <string.h>
#include <stdio.h>
#include <errno.h>
Chris Allegretta's avatar
Chris Allegretta committed
31
#include <unistd.h>
32
#include <ctype.h>
33

34
#ifndef DISABLE_NANORC
35

36
37
38
39
#ifndef RCFILE_NAME
#define RCFILE_NAME ".nanorc"
#endif

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
40
static const rcoption rcopts[] = {
41
    {"boldtext", BOLD_TEXT},
42
43
44
#ifdef ENABLE_LINENUMBERS
    {"linenumbers", LINE_NUMBERS},
#endif
45
46
#ifndef DISABLE_JUSTIFY
    {"brackets", 0},
47
#endif
48
49
    {"const", CONST_UPDATE},  /* deprecated form, remove in 2018 */
    {"constantshow", CONST_UPDATE},
Chris Allegretta's avatar
Chris Allegretta committed
50
#ifndef DISABLE_WRAPJUSTIFY
51
    {"fill", 0},
Chris Allegretta's avatar
Chris Allegretta committed
52
#endif
53
54
#ifndef DISABLE_HISTORIES
    {"historylog", HISTORYLOG},
55
#endif
56
    {"morespace", MORE_SPACE},
57
#ifndef DISABLE_MOUSE
58
    {"mouse", USE_MOUSE},
59
#endif
60
#ifndef DISABLE_MULTIBUFFER
61
62
63
    {"multibuffer", MULTIBUFFER},
#endif
    {"nohelp", NO_HELP},
64
    {"nonewlines", NO_NEWLINES},
Chris Allegretta's avatar
Chris Allegretta committed
65
#ifndef DISABLE_WRAPPING
66
    {"nowrap", NO_WRAP},
Chris Allegretta's avatar
Chris Allegretta committed
67
#endif
68
#ifndef DISABLE_OPERATINGDIR
69
    {"operatingdir", 0},
70
71
#endif
#ifndef DISABLE_HISTORIES
72
73
    {"poslog", POS_HISTORY},  /* deprecated form, remove in 2018 */
    {"positionlog", POS_HISTORY},
74
#endif
Chris Allegretta's avatar
Chris Allegretta committed
75
    {"preserve", PRESERVE},
76
#ifndef DISABLE_JUSTIFY
77
    {"punct", 0},
78
79
    {"quotestr", 0},
#endif
80
    {"rebinddelete", REBIND_DELETE},
81
    {"rebindkeypad", REBIND_KEYPAD},
82
83
84
#ifdef HAVE_REGEX_H
    {"regexp", USE_REGEXP},
#endif
85
#ifndef DISABLE_SPELLER
86
    {"speller", 0},
87
88
89
#endif
    {"suspend", SUSPEND},
    {"tabsize", 0},
90
    {"tempfile", TEMP_FILE},
91
    {"view", VIEW_MODE},
92
#ifndef NANO_TINY
93
    {"allow_insecure_backup", INSECURE_BACKUP},
94
95
96
97
98
99
    {"autoindent", AUTOINDENT},
    {"backup", BACKUP_FILE},
    {"backupdir", 0},
    {"backwards", BACKWARDS_SEARCH},
    {"casesensitive", CASE_SENSITIVE},
    {"cut", CUT_TO_END},
100
    {"justifytrim", JUSTIFY_TRIM},
101
    {"locking", LOCKING},
102
    {"matchbrackets", 0},
103
    {"noconvert", NO_CONVERT},
104
    {"quickblank", QUICK_BLANK},
105
    {"quiet", QUIET},
106
    {"showcursor", SHOW_CURSOR},
107
108
    {"smarthome", SMART_HOME},
    {"smooth", SMOOTH_SCROLL},
109
    {"softwrap", SOFTWRAP},
110
    {"tabstospaces", TABS_TO_SPACES},
111
    {"unix", MAKE_IT_UNIX},
112
    {"whitespace", 0},
113
    {"wordbounds", WORD_BOUNDS},
114
    {"wordchars", 0},
115
116
117
#endif
#ifndef DISABLE_COLOR
    {"titlecolor", 0},
118
    {"numbercolor", 0},
119
120
121
    {"statuscolor", 0},
    {"keycolor", 0},
    {"functioncolor", 0},
122
#endif
123
    {NULL, 0}
124
};
125

126
static bool errors = FALSE;
127
	/* Whether we got any errors while parsing an rcfile. */
128
static size_t lineno = 0;
129
	/* If we did, the line number where the last error occurred. */
130
static char *nanorc = NULL;
131
	/* The path to the rcfile we're parsing. */
132
#ifndef DISABLE_COLOR
133
134
135
static bool opensyntax = FALSE;
	/* Whether we're allowed to add to the last syntax.  When a file ends,
	 * or when a new syntax command is seen, this bool becomes FALSE. */
136
static syntaxtype *live_syntax;
137
	/* The syntax that is currently being parsed. */
138
static colortype *lastcolor = NULL;
139
140
	/* The end of the color list for the current syntax. */
#endif
141

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
142
143
144
/* We have an error in some part of the rcfile.  Print the error message
 * on stderr, and then make the user hit Enter to continue starting
 * nano. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
145
void rcfile_error(const char *msg, ...)
146
147
148
{
    va_list ap;

149
150
151
    if (ISSET(QUIET))
	return;

152
    fprintf(stderr, "\n");
153
154
    if (lineno > 0) {
	errors = TRUE;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
155
	fprintf(stderr, _("Error in %s on line %lu: "), nanorc, (unsigned long)lineno);
156
    }
Chris Allegretta's avatar
Chris Allegretta committed
157

158
    va_start(ap, msg);
159
    vfprintf(stderr, _(msg), ap);
160
    va_end(ap);
161
162

    fprintf(stderr, "\n");
163
}
164
#endif /* !DISABLE_NANORC */
165

166
#if !defined(DISABLE_NANORC) || !defined(DISABLE_HISTORIES)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
167
168
169
/* Parse the next word from the string, null-terminate it, and return
 * a pointer to the first character after the null terminator.  The
 * returned pointer will point to '\0' if we hit the end of the line. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
170
char *parse_next_word(char *ptr)
171
{
172
    while (!isblank(*ptr) && *ptr != '\0')
173
174
175
	ptr++;

    if (*ptr == '\0')
176
	return ptr;
177

178
179
    /* Null-terminate and advance ptr. */
    *ptr++ = '\0';
180

181
    while (isblank(*ptr))
182
183
184
185
	ptr++;

    return ptr;
}
186
#endif /* !DISABLE_NANORC || !DISABLE_HISTORIES */
187

188
#ifndef DISABLE_NANORC
189
190
191
192
/* Parse an argument, with optional quotes, after a keyword that takes
 * one.  If the next word starts with a ", we say that it ends with the
 * last " of the line.  Otherwise, we interpret it as usual, so that the
 * arguments can contain "'s too. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
193
194
char *parse_argument(char *ptr)
{
195
    const char *ptr_save = ptr;
Chris Allegretta's avatar
Chris Allegretta committed
196
197
198
199
200
201
202
203
204
205
206
    char *last_quote = NULL;

    assert(ptr != NULL);

    if (*ptr != '"')
	return parse_next_word(ptr);

    do {
	ptr++;
	if (*ptr == '"')
	    last_quote = ptr;
207
    } while (*ptr != '\0');
Chris Allegretta's avatar
Chris Allegretta committed
208
209
210
211
212
213

    if (last_quote == NULL) {
	if (*ptr == '\0')
	    ptr = NULL;
	else
	    *ptr++ = '\0';
214
	rcfile_error(N_("Argument '%s' has an unterminated \""), ptr_save);
Chris Allegretta's avatar
Chris Allegretta committed
215
216
217
218
219
    } else {
	*last_quote = '\0';
	ptr = last_quote + 1;
    }
    if (ptr != NULL)
220
	while (isblank(*ptr))
Chris Allegretta's avatar
Chris Allegretta committed
221
222
223
224
	    ptr++;
    return ptr;
}

225
#ifndef DISABLE_COLOR
226
227
/* Pass over the current regex string in the line starting at ptr,
 * null-terminate it, and return a pointer to the /next/ word. */
228
229
char *parse_next_regex(char *ptr)
{
230
231
    assert(ptr != NULL);

232
233
234
235
    /* Continue until the end of line, or until a " followed by a
     * blank character or the end of line. */
    while (*ptr != '\0' && (*ptr != '"' ||
		(*(ptr + 1) != '\0' && !isblank(*(ptr + 1)))))
236
237
	ptr++;

238
239
240
    assert(*ptr == '"' || *ptr == '\0');

    if (*ptr == '\0') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
241
242
	rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
243
	return NULL;
244
    }
245

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
246
    /* Null-terminate and advance ptr. */
247
248
    *ptr++ = '\0';

249
    while (isblank(*ptr))
250
251
252
253
254
	ptr++;

    return ptr;
}

255
/* Compile the regular expression regex to see if it's valid.  Return
256
 * TRUE if it is, and FALSE otherwise. */
257
bool nregcomp(const char *regex, int compile_flags)
258
{
259
    regex_t preg;
260
    const char *r = fixbounds(regex);
261
    int rc = regcomp(&preg, r, compile_flags);
262
263

    if (rc != 0) {
264
	size_t len = regerror(rc, &preg, NULL, 0);
265
266
	char *str = charalloc(len);

267
	regerror(rc, &preg, str, len);
268
	rcfile_error(N_("Bad regex \"%s\": %s"), r, str);
269
270
	free(str);
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
271

272
    regfree(&preg);
273
    return (rc == 0);
274
275
}

276
277
/* Parse the next syntax name and its possible extension regexes from the
 * line at ptr, and add it to the global linked list of color syntaxes. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
278
void parse_syntax(char *ptr)
279
{
280
281
    char *nameptr;
	/* A pointer to what should be the name of the syntax. */
282

283
284
    opensyntax = FALSE;

285
    assert(ptr != NULL);
286

287
288
289
    /* Check that the syntax name is not empty. */
    if (*ptr == '\0' || (*ptr == '"' &&
			(*(ptr + 1) == '\0' || *(ptr + 1) == '"'))) {
290
	rcfile_error(N_("Missing syntax name"));
291
	return;
292
    }
293

294
295
296
297
298
299
    nameptr = ++ptr;
    ptr = parse_next_word(ptr);

    /* Check that the name starts and ends with a double quote. */
    if (*(nameptr - 1) != '\x22' || nameptr[strlen(nameptr) - 1] != '\x22') {
	rcfile_error(N_("A syntax name must be quoted"));
300
	return;
301
    }
302

303
304
    /* Strip the end quote. */
    nameptr[strlen(nameptr) - 1] = '\0';
305

306
307
308
309
310
311
    /* Redefining the "none" syntax is not allowed. */
    if (strcmp(nameptr, "none") == 0) {
	rcfile_error(N_("The \"none\" syntax is reserved"));
	return;
    }

312
    /* Initialize a new syntax struct. */
313
314
315
316
317
318
319
    live_syntax = (syntaxtype *)nmalloc(sizeof(syntaxtype));
    live_syntax->name = mallocstrcpy(NULL, nameptr);
    live_syntax->extensions = NULL;
    live_syntax->headers = NULL;
    live_syntax->magics = NULL;
    live_syntax->linter = NULL;
    live_syntax->formatter = NULL;
320
    live_syntax->comment = NULL;
321
    live_syntax->color = NULL;
322
    lastcolor = NULL;
323
    live_syntax->nmultis = 0;
324
325

    /* Hook the new syntax in at the top of the list. */
326
327
    live_syntax->next = syntaxes;
    syntaxes = live_syntax;
328

329
330
    opensyntax = TRUE;

331
#ifdef DEBUG
332
    fprintf(stderr, "Starting a new syntax type: \"%s\"\n", nameptr);
333
334
#endif

335
    /* The default syntax should have no associated extensions. */
336
    if (strcmp(live_syntax->name, "default") == 0 && *ptr != '\0') {
337
	rcfile_error(
338
		N_("The \"default\" syntax does not accept extensions"));
339
340
341
	return;
    }

342
343
    /* If there seem to be extension regexes, pick them up. */
    if (*ptr != '\0')
344
	grab_and_store("extension", ptr, &live_syntax->extensions);
345
}
346
#endif /* !DISABLE_COLOR */
347

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Check whether the given executable function is "universal" (meaning
 * any horizontal movement or deletion) and thus is present in almost
 * all menus. */
bool is_universal(void (*func))
{
    if (func == do_left || func == do_right ||
	func == do_home || func == do_end ||
	func == do_prev_word_void || func == do_next_word_void ||
	func == do_verbatim_input || func == do_cut_text_void ||
	func == do_delete || func == do_backspace ||
	func == do_tab || func == do_enter)
	return TRUE;
    else
	return FALSE;
}

364
/* Bind or unbind a key combo, to or from a function. */
365
void parse_binding(char *ptr, bool dobind)
366
367
{
    char *keyptr = NULL, *keycopy = NULL, *funcptr = NULL, *menuptr = NULL;
368
    sc *s, *newsc = NULL;
369
    int menu;
370
371
372

    assert(ptr != NULL);

373
374
375
376
#ifdef DEBUG
    fprintf(stderr, "Starting the rebinding code...\n");
#endif

377
378
379
380
381
382
383
384
    if (*ptr == '\0') {
	rcfile_error(N_("Missing key name"));
	return;
    }

    keyptr = ptr;
    ptr = parse_next_word(ptr);
    keycopy = mallocstrcpy(NULL, keyptr);
385
386
387

    if (strlen(keycopy) < 2) {
	rcfile_error(N_("Key name is too short"));
Benno Schulenberg's avatar
Benno Schulenberg committed
388
	goto free_copy;
389
390
391
392
393
394
395
396
397
398
    }

    /* Uppercase only the first two or three characters of the key name. */
    keycopy[0] = toupper(keycopy[0]);
    keycopy[1] = toupper(keycopy[1]);
    if (keycopy[0] == 'M' && keycopy[1] == '-') {
	if (strlen(keycopy) > 2)
	    keycopy[2] = toupper(keycopy[2]);
	else {
	    rcfile_error(N_("Key name is too short"));
Benno Schulenberg's avatar
Benno Schulenberg committed
399
	    goto free_copy;
400
401
	}
    }
402

403
404
405
406
407
    /* Allow the codes for Insert and Delete to be rebound, but apart
     * from those two only Control, Meta and Function sequences. */
    if (!strcasecmp(keycopy, "Ins") || !strcasecmp(keycopy, "Del"))
	keycopy[1] = tolower(keycopy[1]);
    else if (keycopy[0] != '^' && keycopy[0] != 'M' && keycopy[0] != 'F') {
408
	rcfile_error(N_("Key name must begin with \"^\", \"M\", or \"F\""));
Benno Schulenberg's avatar
Benno Schulenberg committed
409
	goto free_copy;
410
    } else if ((keycopy[0] == 'M' && keycopy[1] != '-') ||
411
412
		(keycopy[0] == '^' && ((keycopy[1] < 'A' || keycopy[1] > 'z') ||
		keycopy[1] == '[' || keycopy[1] == '`' ||
413
414
415
		(strlen(keycopy) > 2 && strcmp(keycopy, "^Space") != 0))) ||
		(strlen(keycopy) > 3 && strcmp(keycopy, "^Space") != 0 &&
		strcmp(keycopy, "M-Space") != 0)) {
416
417
	rcfile_error(N_("Key name %s is invalid"), keycopy);
	goto free_copy;
418
419
    }

420
421
422
    if (dobind) {
	funcptr = ptr;
	ptr = parse_next_word(ptr);
423

424
	if (funcptr[0] == '\0') {
425
	    rcfile_error(N_("Must specify a function to bind the key to"));
Benno Schulenberg's avatar
Benno Schulenberg committed
426
	    goto free_copy;
427
	}
428
429
430
431
432
    }

    menuptr = ptr;
    ptr = parse_next_word(ptr);

433
    if (menuptr[0] == '\0') {
434
	/* TRANSLATORS: Do not translate the word "all". */
435
	rcfile_error(N_("Must specify a menu (or \"all\") in which to bind/unbind the key"));
Benno Schulenberg's avatar
Benno Schulenberg committed
436
	goto free_copy;
437
    }
438
439

    if (dobind) {
440
	newsc = strtosc(funcptr);
441
	if (newsc == NULL) {
442
	    rcfile_error(N_("Cannot map name \"%s\" to a function"), funcptr);
Benno Schulenberg's avatar
Benno Schulenberg committed
443
	    goto free_copy;
444
	}
445
446
    }

447
448
449
    menu = strtomenu(menuptr);
    if (menu < 1) {
	rcfile_error(N_("Cannot map name \"%s\" to a menu"), menuptr);
Benno Schulenberg's avatar
Benno Schulenberg committed
450
	goto free_copy;
451
452
    }

453
#ifdef DEBUG
454
    if (dobind)
455
456
	fprintf(stderr, "newsc address is now %ld, assigned func = %ld, menu = %x\n",
	    (long)&newsc, (long)newsc->scfunc, menu);
457
    else
458
	fprintf(stderr, "unbinding \"%s\" from menu %x\n", keycopy, menu);
459
460
#endif

461
    if (dobind) {
462
463
464
465
466
467
468
469
	subnfunc *f;
	int mask = 0;

	/* Tally up the menus where the function exists. */
	for (f = allfuncs; f != NULL; f = f->next)
	    if (f->scfunc == newsc->scfunc)
		mask = mask | f->menus;

470
#ifndef NANO_TINY
471
472
473
	/* Handle the special case of the toggles. */
	if (newsc->scfunc == do_toggle_void)
	    mask = MMAIN;
474
#endif
475

476
477
478
479
480
481
482
483
484
	/* Now limit the given menu to those where the function exists. */
	if (is_universal(newsc->scfunc))
	    menu = menu & MMOST;
	else
	    menu = menu & mask;

	if (!menu) {
	    rcfile_error(N_("Function '%s' does not exist in menu '%s'"), funcptr, menuptr);
	    free(newsc);
Benno Schulenberg's avatar
Benno Schulenberg committed
485
	    goto free_copy;
486
487
	}

488
	newsc->menus = menu;
489
	assign_keyinfo(newsc, keycopy, 0);
490

491
	/* Do not allow rebinding a frequent escape-sequence starter: Esc [. */
492
	if (newsc->meta && newsc->keycode == 91) {
493
494
	    rcfile_error(N_("Sorry, keystroke \"%s\" may not be rebound"), newsc->keystr);
	    free(newsc);
Benno Schulenberg's avatar
Benno Schulenberg committed
495
	    goto free_copy;
496
	}
497
498
#ifdef DEBUG
	fprintf(stderr, "s->keystr = \"%s\"\n", newsc->keystr);
499
	fprintf(stderr, "s->keycode = \"%d\"\n", newsc->keycode);
500
#endif
501
    }
502

503
    /* Now find and delete any existing same shortcut in the menu(s). */
504
    for (s = sclist; s != NULL; s = s->next) {
505
	if ((s->menus & menu) && !strcmp(s->keystr, keycopy)) {
506
#ifdef DEBUG
507
	    fprintf(stderr, "deleting entry from among menus %x\n", s->menus);
508
#endif
509
	    s->menus &= ~menu;
510
511
	}
    }
512
513

    if (dobind) {
514
#ifndef NANO_TINY
515
516
517
	/* If this is a toggle, copy its sequence number. */
	if (newsc->scfunc == do_toggle_void) {
	    for (s = sclist; s != NULL; s = s->next)
518
		if (s->scfunc == do_toggle_void && s->toggle == newsc->toggle)
519
520
521
		    newsc->ordinal = s->ordinal;
	} else
	    newsc->ordinal = 0;
522
#endif
523
524
525
	/* Add the new shortcut at the start of the list. */
	newsc->next = sclist;
	sclist = newsc;
Benno Schulenberg's avatar
Benno Schulenberg committed
526
527
528
529
530
	return;
    }

  free_copy:
    free(keycopy);
531
532
}

533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/* Verify that the given file is not a folder nor a device. */
bool is_good_file(char *file)
{
    struct stat rcinfo;

    /* If the thing exists, it may not be a directory nor a device. */
    if (stat(file, &rcinfo) != -1 && (S_ISDIR(rcinfo.st_mode) ||
		S_ISCHR(rcinfo.st_mode) || S_ISBLK(rcinfo.st_mode))) {
	rcfile_error(S_ISDIR(rcinfo.st_mode) ? _("\"%s\" is a directory") :
					_("\"%s\" is a device file"), file);
	return FALSE;
    } else
	return TRUE;
}

548
#ifndef DISABLE_COLOR
549
550
/* Read and parse one included syntax file. */
static void parse_one_include(char *file)
551
552
553
554
{
    FILE *rcstream;

    /* Don't open directories, character files, or block files. */
555
    if (!is_good_file(file))
556
	return;
557

558
559
560
561
562
    /* Open the included syntax file. */
    rcstream = fopen(file, "rb");

    if (rcstream == NULL) {
	rcfile_error(_("Error reading %s: %s"), file, strerror(errno));
563
	return;
564
565
    }

566
    /* Use the name and line number position of the included syntax file
567
     * while parsing it, so we can know where any errors in it are. */
568
    nanorc = file;
569
570
    lineno = 0;

571
#ifdef DEBUG
572
    fprintf(stderr, "Parsing file \"%s\"\n", file);
573
574
#endif

575
    parse_rcfile(rcstream, TRUE);
576
577
}

578
579
/* Expand globs in the passed name, and parse the resultant files. */
void parse_includes(char *ptr)
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
{
    char *option, *nanorc_save = nanorc, *expanded;
    size_t lineno_save = lineno, i;
    glob_t files;

    option = ptr;
    if (*option == '"')
	option++;
    ptr = parse_argument(ptr);

    /* Expand tildes first, then the globs. */
    expanded = real_dir_from_tilde(option);

    if (glob(expanded, GLOB_ERR|GLOB_NOSORT, NULL, &files) == 0) {
	for (i = 0; i < files.gl_pathc; ++i)
595
596
	    parse_one_include(files.gl_pathv[i]);
    } else
597
	rcfile_error(_("Error expanding %s: %s"), option, strerror(errno));
598

599
600
601
    globfree(&files);
    free(expanded);

602
    /* We're done with the included file(s).  Restore the original
603
604
605
606
607
     * filename and line number position. */
    nanorc = nanorc_save;
    lineno = lineno_save;
}

608
609
/* Return the short value corresponding to the color named in colorname,
 * and set bright to TRUE if that color is bright. */
610
short color_to_short(const char *colorname, bool *bright)
611
612
613
614
615
616
617
618
619
{
    assert(colorname != NULL && bright != NULL);

    if (strncasecmp(colorname, "bright", 6) == 0) {
	*bright = TRUE;
	colorname += 6;
    }

    if (strcasecmp(colorname, "green") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
620
	return COLOR_GREEN;
621
    else if (strcasecmp(colorname, "red") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
622
	return COLOR_RED;
623
    else if (strcasecmp(colorname, "blue") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
624
	return COLOR_BLUE;
625
    else if (strcasecmp(colorname, "white") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
626
	return COLOR_WHITE;
627
    else if (strcasecmp(colorname, "yellow") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
628
	return COLOR_YELLOW;
629
    else if (strcasecmp(colorname, "cyan") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
630
	return COLOR_CYAN;
631
    else if (strcasecmp(colorname, "magenta") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
632
	return COLOR_MAGENTA;
633
    else if (strcasecmp(colorname, "black") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
634
635
636
	return COLOR_BLACK;

    rcfile_error(N_("Color \"%s\" not understood.\n"
637
638
639
640
		"Valid colors are \"green\", \"red\", \"blue\",\n"
		"\"white\", \"yellow\", \"cyan\", \"magenta\" and\n"
		"\"black\", with the optional prefix \"bright\"\n"
		"for foreground colors."), colorname);
Benno Schulenberg's avatar
Benno Schulenberg committed
641
    return -1;
642
643
}

644
/* Parse the color string in the line at ptr, and add it to the current
645
646
647
 * file's associated colors.  rex_flags are the regex compilation flags
 * to use, excluding or including REG_ICASE for case (in)sensitivity. */
void parse_colors(char *ptr, int rex_flags)
648
{
649
    short fg, bg;
650
    bool bright = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
651
    char *fgstr;
652

653
    assert(ptr != NULL);
654

655
    if (!opensyntax) {
656
	rcfile_error(
657
658
		N_("A '%s' command requires a preceding 'syntax' command"),
		"color");
659
660
661
	return;
    }

662
    if (*ptr == '\0') {
663
	rcfile_error(N_("Missing color name"));
664
	return;
665
666
    }

667
668
    fgstr = ptr;
    ptr = parse_next_word(ptr);
669
670
    if (!parse_color_names(fgstr, &fg, &bg, &bright))
	return;
671

672
    if (*ptr == '\0') {
673
	rcfile_error(N_("Missing regex string after '%s' command"), "color");
674
675
676
	return;
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
677
    /* Now for the fun part.  Start adding regexes to individual strings
678
679
     * in the colorstrings array, woo! */
    while (ptr != NULL && *ptr != '\0') {
680
	colortype *newcolor = NULL;
681
	    /* The container for a color plus its regexes. */
Benno Schulenberg's avatar
Benno Schulenberg committed
682
683
	bool goodstart;
	    /* Whether the start expression was valid. */
684
	bool expectend = FALSE;
Benno Schulenberg's avatar
Benno Schulenberg committed
685
	    /* Whether to expect an end= line. */
686

687
	if (strncasecmp(ptr, "start=", 6) == 0) {
688
	    ptr += 6;
689
	    expectend = TRUE;
690
691
692
	}

	if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
693
694
	    rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
695
	    ptr = parse_next_regex(ptr);
696
697
	    continue;
	}
698

699
	fgstr = ++ptr;
700
	ptr = parse_next_regex(ptr);
701
702
703
	if (ptr == NULL)
	    break;

704
	goodstart = nregcomp(fgstr, rex_flags);
Benno Schulenberg's avatar
Benno Schulenberg committed
705
706
707
708

	/* If the starting regex is valid, initialize a new color struct,
	 * and hook it in at the tail of the linked list. */
	if (goodstart) {
709
710
	    newcolor = (colortype *)nmalloc(sizeof(colortype));

711
712
713
	    newcolor->fg = fg;
	    newcolor->bg = bg;
	    newcolor->bright = bright;
714
	    newcolor->rex_flags = rex_flags;
715
716
717
718

	    newcolor->start_regex = mallocstrcpy(NULL, fgstr);
	    newcolor->start = NULL;

719
	    newcolor->end_regex = NULL;
720
	    newcolor->end = NULL;
721

722
	    newcolor->next = NULL;
723

Chris Allegretta's avatar
Chris Allegretta committed
724
#ifdef DEBUG
Benno Schulenberg's avatar
Benno Schulenberg committed
725
	    fprintf(stderr, "Adding an entry for fg %hd, bg %hd\n", fg, bg);
Chris Allegretta's avatar
Chris Allegretta committed
726
#endif
727
	    if (lastcolor == NULL)
Benno Schulenberg's avatar
Benno Schulenberg committed
728
		live_syntax->color = newcolor;
729
	    else
730
		lastcolor->next = newcolor;
731

732
	    lastcolor = newcolor;
Benno Schulenberg's avatar
Benno Schulenberg committed
733
	}
Chris Allegretta's avatar
Chris Allegretta committed
734

735
736
	if (!expectend)
	    continue;
737

738
739
740
741
	if (ptr == NULL || strncasecmp(ptr, "end=", 4) != 0) {
	    rcfile_error(N_("\"start=\" requires a corresponding \"end=\""));
	    return;
	}
742

743
744
745
746
747
	ptr += 4;
	if (*ptr != '"') {
	    rcfile_error(N_("Regex strings must begin and end with a \" character"));
	    continue;
	}
748

749
750
751
	fgstr = ++ptr;
	ptr = parse_next_regex(ptr);
	if (ptr == NULL)
752
	    break;
753

754
755
	/* If the start regex was invalid, skip past the end regex
	 * to stay in sync. */
Benno Schulenberg's avatar
Benno Schulenberg committed
756
	if (!goodstart)
757
758
759
	    continue;

	/* If it's valid, save the ending regex string. */
760
	if (nregcomp(fgstr, rex_flags))
761
762
763
764
765
	    newcolor->end_regex = mallocstrcpy(NULL, fgstr);

	/* Lame way to skip another static counter. */
	newcolor->id = live_syntax->nmultis;
	live_syntax->nmultis++;
766
    }
767
}
768

769
770
771
772
773
774
/* Parse the color name, or pair of color names, in combostr. */
bool parse_color_names(char *combostr, short *fg, short *bg, bool *bright)
{
    bool no_fgcolor = FALSE;

    if (combostr == NULL)
775
	return FALSE;
776
777
778
779
780
781
782
783
784
785
786
787
788

    if (strchr(combostr, ',') != NULL) {
	char *bgcolorname;
	strtok(combostr, ",");
	bgcolorname = strtok(NULL, ",");
	if (bgcolorname == NULL) {
	    /* If we have a background color without a foreground color,
	     * parse it properly. */
	    bgcolorname = combostr + 1;
	    no_fgcolor = TRUE;
	}
	if (strncasecmp(bgcolorname, "bright", 6) == 0) {
	    rcfile_error(N_("Background color \"%s\" cannot be bright"), bgcolorname);
789
	    return FALSE;
790
791
792
793
794
795
796
797
798
799
	}
	*bg = color_to_short(bgcolorname, bright);
    } else
	*bg = -1;

    if (!no_fgcolor) {
	*fg = color_to_short(combostr, bright);

	/* Don't try to parse screwed-up foreground colors. */
	if (*fg == -1)
800
	    return FALSE;
801
802
803
    } else
	*fg = -1;

804
    return TRUE;
805
806
}

807
808
/* Read regex strings enclosed in double quotes from the line pointed at
 * by ptr, and store them quoteless in the passed storage place. */
809
void grab_and_store(const char *kind, char *ptr, regexlisttype **storage)
810
{
811
    regexlisttype *lastthing;
812

813
    if (!opensyntax) {
814
	rcfile_error(
815
		N_("A '%s' command requires a preceding 'syntax' command"), kind);
816
817
818
	return;
    }

819
820
    /* The default syntax doesn't take any file matching stuff. */
    if (strcmp(live_syntax->name, "default") == 0 && *ptr != '\0') {
821
	rcfile_error(
822
		N_("The \"default\" syntax does not accept '%s' regexes"), kind);
823
824
825
826
	return;
    }

    if (*ptr == '\0') {
827
	rcfile_error(N_("Missing regex string after '%s' command"), kind);
828
829
830
	return;
    }

831
832
833
834
835
836
    lastthing = *storage;

    /* If there was an earlier command, go to the last of those regexes. */
    while (lastthing != NULL && lastthing->next != NULL)
	lastthing = lastthing->next;

837
    /* Now gather any valid regexes and add them to the linked list. */
838
839
    while (*ptr != '\0') {
	const char *regexstring;
840
	regexlisttype *newthing;
841
842
843
844

	if (*ptr != '"') {
	    rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
845
	    return;
846
847
	}

848
	regexstring = ++ptr;
849
850
	ptr = parse_next_regex(ptr);
	if (ptr == NULL)
851
	    return;
852

853
	/* If the regex string is malformed, skip it. */
854
	if (!nregcomp(regexstring, NANO_REG_EXTENDED | REG_NOSUB))
855
	    continue;
856

857
858
859
860
	/* Copy the regex into a struct, and hook this in at the end. */
	newthing = (regexlisttype *)nmalloc(sizeof(regexlisttype));
	newthing->full_regex = mallocstrcpy(NULL, regexstring);
	newthing->next = NULL;
861

862
863
864
865
	if (lastthing == NULL)
	    *storage = newthing;
	else
	    lastthing->next = newthing;
866

867
	lastthing = newthing;
868
869
    }
}
870

871
872
/* Parse and store the name given after a linter/formatter command. */
void pick_up_name(const char *kind, char *ptr, char **storage)
873
874
875
{
    assert(ptr != NULL);

876
    if (!opensyntax) {
877
	rcfile_error(
878
		N_("A '%s' command requires a preceding 'syntax' command"), kind);
879
880
881
882
	return;
    }

    if (*ptr == '\0') {
883
	rcfile_error(N_("Missing command after '%s'"), kind);
884
885
886
	return;
    }

887
    free(*storage);
888

889
    /* Allow unsetting the command by using an empty string. */
890
    if (!strcmp(ptr, "\"\""))
891
	*storage = NULL;
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
    else if (*ptr == '"') {
	*storage = mallocstrcpy(NULL, ++ptr);
	char* q = *storage;
	char* p = *storage;
	/* Snip out the backslashes of escaped characters. */
	while (*p != '"') {
	    if (*p == '\0') {
		rcfile_error(N_("Argument of '%s' lacks closing \""), kind);
		free(*storage);
		*storage = NULL;
		return;
	    } else if (*p == '\\' && *(p + 1) != '\0') {
		p++;
	    }
	    *q++ = *p++;
	}
	*q = '\0';
    }
910
    else
911
	*storage = mallocstrcpy(NULL, ptr);
912
}
913
#endif /* !DISABLE_COLOR */
914

915
916
/* Verify that the user has not unmapped every shortcut for a
 * function that we consider 'vital' (such as "Exit"). */
917
918
919
920
921
static void check_vitals_mapped(void)
{
    subnfunc *f;
    int v;
#define VITALS 5
922
    void (*vitals[VITALS])(void) = { do_exit, do_exit, do_cancel, do_cancel, do_cancel };
923
924
925
    int inmenus[VITALS] = { MMAIN, MHELP, MWHEREIS, MREPLACE, MGOTOLINE };

    for  (v = 0; v < VITALS; v++) {
926
927
928
929
930
	for (f = allfuncs; f != NULL; f = f->next) {
	    if (f->scfunc == vitals[v] && f->menus & inmenus[v]) {
		const sc *s = first_sc_for(inmenus[v], f->scfunc);
		if (!s) {
		    fprintf(stderr, _("Fatal error: no keys mapped for function "
931
					"\"%s\".  Exiting.\n"), f->desc);
932
		    fprintf(stderr, _("If needed, use nano with the -I option "
933
934
					"to adjust your nanorc settings.\n"));
		    exit(1);
935
936
937
938
		}
		break;
	    }
	}
939
940
941
    }
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
942
/* Parse the rcfile, once it has been opened successfully at rcstream,
943
944
945
 * and close it afterwards.  If syntax_only is TRUE, allow the file to
 * to contain only color syntax commands. */
void parse_rcfile(FILE *rcstream, bool syntax_only)
946
{
947
948
    char *buf = NULL;
    ssize_t len;
949
    size_t n = 0;
950
951
952

    while ((len = getline(&buf, &n, rcstream)) > 0) {
	char *ptr, *keyword, *option;
953
954
	int set = 0;
	size_t i;
955

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
956
	/* Ignore the newline. */
957
958
	if (buf[len - 1] == '\n')
	    buf[len - 1] = '\0';
959
960
961

	lineno++;
	ptr = buf;
962
	while (isblank(*ptr))
963
964
	    ptr++;

965
966
967
	/* If we have a blank line or a comment, skip to the next
	 * line. */
	if (*ptr == '\0' || *ptr == '#')
968
969
	    continue;

970
	/* Otherwise, skip to the next space. */
971
972
973
	keyword = ptr;
	ptr = parse_next_word(ptr);

974
#ifndef DISABLE_COLOR
975
976
	/* Handle extending first... */
	if (strcasecmp(keyword, "extendsyntax") == 0) {
977
	    syntaxtype *sint;
978
979
980
	    char *syntaxname = ptr;

	    ptr = parse_next_word(ptr);
981
982
983

	    for (sint = syntaxes; sint != NULL; sint = sint->next)
		if (!strcmp(sint->name, syntaxname))
984
985
		    break;

986
987
988
	    if (sint == NULL) {
		rcfile_error(N_("Could not find syntax \"%s\" to extend"),
				syntaxname);
989
		opensyntax = FALSE;
990
991
		continue;
	    }
992
993
994
995

	    live_syntax = sint;
	    opensyntax = TRUE;

996
	    /* Refind the tail of the color list for this syntax. */
997
998
999
1000
	    lastcolor = sint->color;
	    if (lastcolor != NULL)
		while (lastcolor->next != NULL)
		    lastcolor = lastcolor->next;
1001

1002
1003
	    keyword = ptr;
	    ptr = parse_next_word(ptr);
1004
1005
	}

1006
	/* Try to parse the keyword. */
1007
	if (strcasecmp(keyword, "syntax") == 0) {
1008
	    if (opensyntax && lastcolor == NULL)
1009
		rcfile_error(N_("Syntax \"%s\" has no color commands"),
1010
				live_syntax->name);
Chris Allegretta's avatar
Chris Allegretta committed
1011
	    parse_syntax(ptr);
1012
	}
1013
1014
	else if (strcasecmp(keyword, "header") == 0)
	    grab_and_store("header", ptr, &live_syntax->headers);
1015
	else if (strcasecmp(keyword, "magic") == 0)
1016
#ifdef HAVE_LIBMAGIC
1017
	    grab_and_store("magic", ptr, &live_syntax->magics);
1018
1019
#else
	    ;
1020
1021
1022
1023
1024
1025
#endif
	else if (strcasecmp(keyword, "comment") == 0)
#ifdef ENABLE_COMMENT
	    pick_up_name("comment", ptr, &live_syntax->comment);
#else
	    ;
1026
#endif
1027
	else if (strcasecmp(keyword, "color") == 0)
1028
	    parse_colors(ptr, NANO_REG_EXTENDED);
1029
	else if (strcasecmp(keyword, "icolor") == 0)
1030
	    parse_colors(ptr, NANO_REG_EXTENDED | REG_ICASE);
1031
	else if (strcasecmp(keyword, "linter") == 0)
1032
	    pick_up_name("linter", ptr, &live_syntax->linter);
1033
	else if (strcasecmp(keyword, "formatter") == 0)
1034
#ifndef DISABLE_SPELLER
1035
	    pick_up_name("formatter", ptr, &live_syntax->formatter);
1036
1037
1038
#else
	    ;
#endif
1039
1040
1041
1042
1043
1044
	else if (syntax_only)
	    rcfile_error(N_("Command \"%s\" not allowed in included file"),
					keyword);
	else if (strcasecmp(keyword, "include") == 0)
	    parse_includes(ptr);
	else
1045
#endif /* !DISABLE_COLOR */
1046
1047
1048
1049
	if (strcasecmp(keyword, "set") == 0)
	    set = 1;
	else if (strcasecmp(keyword, "unset") == 0)
	    set = -1;
1050
	else if (strcasecmp(keyword, "bind") == 0)
1051
	    parse_binding(ptr, TRUE);
1052
	else if (strcasecmp(keyword, "unbind") == 0)
1053
	    parse_binding(ptr, FALSE);
1054
	else
1055
	    rcfile_error(N_("Command \"%s\" not understood"), keyword);
1056

1057
#ifndef DISABLE_COLOR
1058
1059
	/* If a syntax was extended, it stops at the end of the command. */
	if (live_syntax != syntaxes)
1060
	    opensyntax = FALSE;
1061
1062
#endif

1063
1064
1065
1066
	if (set == 0)
	    continue;

	if (*ptr == '\0') {
1067
	    rcfile_error(N_("Missing option"));
1068
1069
1070
1071
1072
1073
	    continue;
	}

	option = ptr;
	ptr = parse_next_word(ptr);

1074
	/* Find the just read name among the existing options. */
1075
	for (i = 0; rcopts[i].name != NULL; i++) {
1076
1077
1078
1079
1080
1081
1082
1083
1084
	    if (strcasecmp(option, rcopts[i].name) == 0)
		break;
	}

	if (rcopts[i].name == NULL) {
	    rcfile_error(N_("Unknown option \"%s\""), option);
	    continue;
	}

1085
#ifdef DEBUG
1086
1087
	fprintf(stderr, "    Option name = \"%s\"\n", rcopts[i].name);
	fprintf(stderr, "    Flag = %ld\n", rcopts[i].flag);
1088
#endif
1089
1090
	/* First handle unsetting. */
	if (set == -1) {
1091
	    if (rcopts[i].flag != 0)
1092
		UNSET(rcopts[i].flag);
1093
	    else
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
		rcfile_error(N_("Cannot unset option \"%s\""), rcopts[i].name);
	    continue;
	}

	/* If the option has a flag, it doesn't take an argument. */
	if (rcopts[i].flag != 0) {
	    SET(rcopts[i].flag);
	    continue;
	}

	/* The option doesn't have a flag, so it takes an argument. */
	if (*ptr == '\0') {
	    rcfile_error(N_("Option \"%s\" requires an argument"),
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1107
				rcopts[i].name);
1108
1109
1110
1111
1112
1113
1114
1115
1116
	    continue;
	}

	option = ptr;
	if (*option == '"')
	    option++;
	ptr = parse_argument(ptr);

	option = mallocstrcpy(NULL, option);
Chris Allegretta's avatar
Chris Allegretta committed
1117
#ifdef DEBUG
1118
	fprintf(stderr, "    Option argument = \"%s\"\n", option);
Chris Allegretta's avatar
Chris Allegretta committed
1119
#endif
1120
1121
1122
1123
1124
	/* Make sure the option argument is a valid multibyte string. */
	if (!is_valid_mbstring(option)) {
	    rcfile_error(N_("Option is not a valid multibyte string"));
	    continue;
	}
1125

1126
#ifndef DISABLE_COLOR
1127
1128
	if (strcasecmp(rcopts[i].name, "titlecolor") == 0)
	    specified_color_combo[TITLE_BAR] = option;
1129
1130
	else if (strcasecmp(rcopts[i].name, "numbercolor") == 0)
	    specified_color_combo[LINE_NUMBER] = option;
1131
1132
1133
1134
1135
1136
1137
	else if (strcasecmp(rcopts[i].name, "statuscolor") == 0)
	    specified_color_combo[STATUS_BAR] = option;
	else if (strcasecmp(rcopts[i].name, "keycolor") == 0)
	    specified_color_combo[KEY_COMBO] = option;
	else if (strcasecmp(rcopts[i].name, "functioncolor") == 0)
	    specified_color_combo[FUNCTION_TAG] = option;
	else
1138
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1139
#ifndef DISABLE_OPERATINGDIR
1140
1141
1142
	if (strcasecmp(rcopts[i].name, "operatingdir") == 0)
	    operating_dir = option;
	else
Chris Allegretta's avatar
Chris Allegretta committed
1143
#endif
1144
#ifndef DISABLE_WRAPJUSTIFY
1145
1146
1147
1148
1149
	if (strcasecmp(rcopts[i].name, "fill") == 0) {
	    if (!parse_num(option, &wrap_at)) {
		rcfile_error(N_("Requested fill size \"%s\" is invalid"),
				option);
		wrap_at = -CHARS_FROM_EOL;
1150
1151
	    } else {
		UNSET(NO_WRAP);
1152
		free(option);
1153
	    }
1154
	} else
Chris Allegretta's avatar
Chris Allegretta committed
1155
#endif
1156
#ifndef NANO_TINY
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
	if (strcasecmp(rcopts[i].name, "matchbrackets") == 0) {
	    matchbrackets = option;
	    if (has_blank_mbchars(matchbrackets)) {
		rcfile_error(N_("Non-blank characters required"));
		free(matchbrackets);
		matchbrackets = NULL;
	    }
	} else if (strcasecmp(rcopts[i].name, "whitespace") == 0) {
	    whitespace = option;
	    if (mbstrlen(whitespace) != 2 || strlenpt(whitespace) != 2) {
		rcfile_error(N_("Two single-column characters required"));
		free(whitespace);
		whitespace = NULL;
	    } else {
		whitespace_len[0] = parse_mbchar(whitespace, NULL, NULL);
		whitespace_len[1] = parse_mbchar(whitespace +
1173
					whitespace_len[0], NULL, NULL);
1174
1175
	    }
	} else
1176
#endif
1177
#ifndef DISABLE_JUSTIFY
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
	if (strcasecmp(rcopts[i].name, "punct") == 0) {
	    punct = option;
	    if (has_blank_mbchars(punct)) {
		rcfile_error(N_("Non-blank characters required"));
		free(punct);
		punct = NULL;
	    }
	} else if (strcasecmp(rcopts[i].name, "brackets") == 0) {
	    brackets = option;
	    if (has_blank_mbchars(brackets)) {
		rcfile_error(N_("Non-blank characters required"));
		free(brackets);
		brackets = NULL;
	    }
	} else if (strcasecmp(rcopts[i].name, "quotestr") == 0)
	    quotestr = option;
	else
1195
#endif
1196
#ifndef NANO_TINY
1197
1198
1199
	if (strcasecmp(rcopts[i].name, "backupdir") == 0)
	    backup_dir = option;
	else
1200
1201
1202
	if (strcasecmp(rcopts[i].name, "wordchars") == 0)
	    word_chars = option;
	else
1203
#endif
1204
#ifndef DISABLE_SPELLER
1205
1206
1207
	if (strcasecmp(rcopts[i].name, "speller") == 0)
	    alt_speller = option;
	else
1208
#endif
1209
1210
1211
1212
1213
1214
1215
1216
1217
	if (strcasecmp(rcopts[i].name, "tabsize") == 0) {
	    if (!parse_num(option, &tabsize) || tabsize <= 0) {
		rcfile_error(N_("Requested tab size \"%s\" is invalid"),
				option);
		tabsize = -1;
	    } else
		free(option);
	} else
	    assert(FALSE);
1218
    }
1219

1220
#ifndef DISABLE_COLOR
1221
    if (opensyntax && lastcolor == NULL)
1222
	rcfile_error(N_("Syntax \"%s\" has no color commands"),
1223
			live_syntax->name);
1224

1225
    opensyntax = FALSE;
1226
#endif
1227

Chris Allegretta's avatar
Chris Allegretta committed
1228
    free(buf);
1229
1230
    fclose(rcstream);
    lineno = 0;
1231

1232
1233
1234
    return;
}

1235
1236
/* Read and interpret one of the two nanorc files. */
void parse_one_nanorc(void)
1237
1238
1239
{
    FILE *rcstream;

1240
    /* Don't try to open directories nor devices. */
1241
    if (!is_good_file(nanorc))
1242
	return;
1243

1244
#ifdef DEBUG
1245
    fprintf(stderr, "Going to parse file \"%s\"\n", nanorc);
1246
1247
#endif

1248
    rcstream = fopen(nanorc, "rb");
1249
1250
1251

    /* If opening the file succeeded, parse it.  Otherwise, only
     * complain if the file actually exists. */
1252
    if (rcstream != NULL)
1253
	parse_rcfile(rcstream, FALSE);
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
    else if (errno != ENOENT)
	rcfile_error(N_("Error reading %s: %s"), nanorc, strerror(errno));
}

/* First read the system-wide rcfile, then the user's rcfile. */
void do_rcfiles(void)
{
    nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc");

    /* Process the system-wide nanorc. */
    parse_one_nanorc();
1265

1266
1267
    /* When configured with --disable-wrapping-as-root, turn wrapping off
     * for root, so that only root's .nanorc or --fill can turn it on. */
1268
#ifdef DISABLE_ROOTWRAPPING
1269
1270
1271
    if (geteuid() == NANO_ROOT_UID)
	SET(NO_WRAP);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1272

1273
    get_homedir();
1274

1275
    if (homedir == NULL)
1276
	rcfile_error(N_("I can't find my home directory!  Wah!"));
1277
    else {
1278
1279
	nanorc = charealloc(nanorc, strlen(homedir) + strlen(RCFILE_NAME) + 2);
	sprintf(nanorc, "%s/%s", homedir, RCFILE_NAME);
1280

1281
1282
	/* Process the current user's nanorc. */
	parse_one_nanorc();
1283
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1284

1285
1286
    check_vitals_mapped();

1287
    free(nanorc);
1288

1289
1290
    if (errors && !ISSET(QUIET)) {
	errors = FALSE;
1291
	fprintf(stderr, _("\nPress Enter to continue starting nano.\n"));
1292
1293
1294
	while (getchar() != '\n')
	    ;
    }
1295
1296
}

1297
#endif /* !DISABLE_NANORC */