rcfile.c 34.5 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},
65
    {"nopauses", NO_PAUSES},
Chris Allegretta's avatar
Chris Allegretta committed
66
#ifndef DISABLE_WRAPPING
67
    {"nowrap", NO_WRAP},
Chris Allegretta's avatar
Chris Allegretta committed
68
#endif
69
#ifndef DISABLE_OPERATINGDIR
70
    {"operatingdir", 0},
71
72
#endif
#ifndef DISABLE_HISTORIES
73
74
    {"poslog", POS_HISTORY},  /* deprecated form, remove in 2018 */
    {"positionlog", POS_HISTORY},
75
#endif
Chris Allegretta's avatar
Chris Allegretta committed
76
    {"preserve", PRESERVE},
77
#ifndef DISABLE_JUSTIFY
78
    {"punct", 0},
79
80
    {"quotestr", 0},
#endif
81
    {"rebinddelete", REBIND_DELETE},
82
    {"rebindkeypad", REBIND_KEYPAD},
83
    {"regexp", USE_REGEXP},
84
#ifndef DISABLE_SPELLER
85
    {"speller", 0},
86
87
88
#endif
    {"suspend", SUSPEND},
    {"tabsize", 0},
89
    {"tempfile", TEMP_FILE},
90
    {"view", VIEW_MODE},
91
#ifndef NANO_TINY
92
    {"allow_insecure_backup", INSECURE_BACKUP},
93
94
95
96
97
98
    {"autoindent", AUTOINDENT},
    {"backup", BACKUP_FILE},
    {"backupdir", 0},
    {"backwards", BACKWARDS_SEARCH},
    {"casesensitive", CASE_SENSITIVE},
    {"cut", CUT_TO_END},
99
    {"justifytrim", JUSTIFY_TRIM},
100
    {"locking", LOCKING},
101
    {"matchbrackets", 0},
102
    {"noconvert", NO_CONVERT},
103
    {"quickblank", QUICK_BLANK},
104
    {"quiet", QUIET},
105
    {"showcursor", SHOW_CURSOR},
106
107
    {"smarthome", SMART_HOME},
    {"smooth", SMOOTH_SCROLL},
108
    {"softwrap", SOFTWRAP},
109
    {"tabstospaces", TABS_TO_SPACES},
110
    {"unix", MAKE_IT_UNIX},
111
    {"whitespace", 0},
112
    {"wordbounds", WORD_BOUNDS},
113
    {"wordchars", 0},
114
115
116
#endif
#ifndef DISABLE_COLOR
    {"titlecolor", 0},
117
    {"numbercolor", 0},
118
119
120
    {"statuscolor", 0},
    {"keycolor", 0},
    {"functioncolor", 0},
121
#endif
122
    {NULL, 0}
123
};
124

125
static bool errors = FALSE;
126
	/* Whether we got any errors while parsing an rcfile. */
127
static size_t lineno = 0;
128
	/* If we did, the line number where the last error occurred. */
129
static char *nanorc = NULL;
130
	/* The path to the rcfile we're parsing. */
131
#ifndef DISABLE_COLOR
132
133
134
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. */
135
static syntaxtype *live_syntax;
136
	/* The syntax that is currently being parsed. */
137
static colortype *lastcolor = NULL;
138
139
	/* The end of the color list for the current syntax. */
#endif
140

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
141
142
143
/* 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
144
void rcfile_error(const char *msg, ...)
145
146
147
{
    va_list ap;

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

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

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

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

165
#if !defined(DISABLE_NANORC) || !defined(DISABLE_HISTORIES)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
166
167
168
/* 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
169
char *parse_next_word(char *ptr)
170
{
171
    while (!isblank((unsigned char)*ptr) && *ptr != '\0')
172
173
174
	ptr++;

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

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

180
    while (isblank((unsigned char)*ptr))
181
182
183
184
	ptr++;

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

187
#ifndef DISABLE_NANORC
188
189
190
191
/* 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
192
193
char *parse_argument(char *ptr)
{
194
    const char *ptr_save = ptr;
Chris Allegretta's avatar
Chris Allegretta committed
195
196
197
198
199
200
201
202
203
204
205
    char *last_quote = NULL;

    assert(ptr != NULL);

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

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

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

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

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

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

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

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

248
    while (isblank((unsigned char)*ptr))
249
250
251
252
253
	ptr++;

    return ptr;
}

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

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

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

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

275
276
/* 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
277
void parse_syntax(char *ptr)
278
{
279
280
    char *nameptr;
	/* A pointer to what should be the name of the syntax. */
281

282
283
    opensyntax = FALSE;

284
    assert(ptr != NULL);
285

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

293
294
295
296
297
298
    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"));
299
	return;
300
    }
301

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

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

311
    /* Initialize a new syntax struct. */
312
313
314
315
316
317
318
    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;
319
    live_syntax->comment = NULL;
320
    live_syntax->color = NULL;
321
    lastcolor = NULL;
322
    live_syntax->nmultis = 0;
323
324

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

328
329
    opensyntax = TRUE;

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

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

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

347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* 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;
}

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

    assert(ptr != NULL);

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

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

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

    if (strlen(keycopy) < 2) {
	rcfile_error(N_("Key name is too short"));
Benno Schulenberg's avatar
Benno Schulenberg committed
387
	goto free_copy;
388
389
390
    }

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

402
403
404
    /* 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"))
405
	keycopy[1] = tolower((unsigned char)keycopy[1]);
406
    else if (keycopy[0] != '^' && keycopy[0] != 'M' && keycopy[0] != 'F') {
407
	rcfile_error(N_("Key name must begin with \"^\", \"M\", or \"F\""));
Benno Schulenberg's avatar
Benno Schulenberg committed
408
	goto free_copy;
409
    } else if ((keycopy[0] == 'M' && keycopy[1] != '-') ||
410
411
		(keycopy[0] == '^' && ((keycopy[1] < 'A' || keycopy[1] > 'z') ||
		keycopy[1] == '[' || keycopy[1] == '`' ||
412
413
414
		(strlen(keycopy) > 2 && strcmp(keycopy, "^Space") != 0))) ||
		(strlen(keycopy) > 3 && strcmp(keycopy, "^Space") != 0 &&
		strcmp(keycopy, "M-Space") != 0)) {
415
416
	rcfile_error(N_("Key name %s is invalid"), keycopy);
	goto free_copy;
417
418
    }

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

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

    menuptr = ptr;
    ptr = parse_next_word(ptr);

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

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

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

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

460
    if (dobind) {
461
462
463
464
465
466
467
468
	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;

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

475
476
477
478
479
480
481
482
483
	/* 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
484
	    goto free_copy;
485
486
	}

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

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

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

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

  free_copy:
    free(keycopy);
530
531
}

532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* 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;
}

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

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

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

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

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

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

574
    parse_rcfile(rcstream, TRUE);
575
576
}

577
578
/* Expand globs in the passed name, and parse the resultant files. */
void parse_includes(char *ptr)
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
{
    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)
594
595
	    parse_one_include(files.gl_pathv[i]);
    } else
596
	rcfile_error(_("Error expanding %s: %s"), option, strerror(errno));
597

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

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

607
608
/* Return the short value corresponding to the color named in colorname,
 * and set bright to TRUE if that color is bright. */
609
short color_to_short(const char *colorname, bool *bright)
610
611
612
613
614
615
616
617
618
{
    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
619
	return COLOR_GREEN;
620
    else if (strcasecmp(colorname, "red") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
621
	return COLOR_RED;
622
    else if (strcasecmp(colorname, "blue") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
623
	return COLOR_BLUE;
624
    else if (strcasecmp(colorname, "white") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
625
	return COLOR_WHITE;
626
    else if (strcasecmp(colorname, "yellow") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
627
	return COLOR_YELLOW;
628
    else if (strcasecmp(colorname, "cyan") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
629
	return COLOR_CYAN;
630
    else if (strcasecmp(colorname, "magenta") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
631
	return COLOR_MAGENTA;
632
    else if (strcasecmp(colorname, "black") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
633
634
635
	return COLOR_BLACK;

    rcfile_error(N_("Color \"%s\" not understood.\n"
636
637
638
639
		"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
640
    return -1;
641
642
}

643
/* Parse the color string in the line at ptr, and add it to the current
644
645
646
 * 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)
647
{
648
    short fg, bg;
649
    bool bright = FALSE;
650
    char *item;
651

652
    assert(ptr != NULL);
653

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

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

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

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

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

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

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

698
	item = ++ptr;
699
	ptr = parse_next_regex(ptr);
700
701
702
	if (ptr == NULL)
	    break;

703
	if (*item == '\0') {
704
705
706
	    rcfile_error(N_("Empty regex string"));
	    goodstart = FALSE;
	} else
707
	    goodstart = nregcomp(item, rex_flags);
Benno Schulenberg's avatar
Benno Schulenberg committed
708
709
710
711

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

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

719
	    newcolor->start_regex = mallocstrcpy(NULL, item);
720
721
	    newcolor->start = NULL;

722
	    newcolor->end_regex = NULL;
723
	    newcolor->end = NULL;
724

725
	    newcolor->next = NULL;
726

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

735
	    lastcolor = newcolor;
Benno Schulenberg's avatar
Benno Schulenberg committed
736
	}
Chris Allegretta's avatar
Chris Allegretta committed
737

738
739
	if (!expectend)
	    continue;
740

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

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

752
	item = ++ptr;
753
754
	ptr = parse_next_regex(ptr);
	if (ptr == NULL)
755
	    break;
756

757
	if (*item == '\0') {
758
759
760
761
	    rcfile_error(N_("Empty regex string"));
	    continue;
	}

762
763
	/* If the start regex was invalid, skip past the end regex
	 * to stay in sync. */
Benno Schulenberg's avatar
Benno Schulenberg committed
764
	if (!goodstart)
765
766
767
	    continue;

	/* If it's valid, save the ending regex string. */
768
769
	if (nregcomp(item, rex_flags))
	    newcolor->end_regex = mallocstrcpy(NULL, item);
770
771
772
773

	/* Lame way to skip another static counter. */
	newcolor->id = live_syntax->nmultis;
	live_syntax->nmultis++;
774
    }
775
}
776

777
778
779
780
781
782
/* 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)
783
	return FALSE;
784
785
786
787
788
789
790
791
792
793
794
795
796

    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);
797
	    return FALSE;
798
799
800
801
802
803
804
805
806
807
	}
	*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)
808
	    return FALSE;
809
810
811
    } else
	*fg = -1;

812
    return TRUE;
813
814
}

815
816
/* Read regex strings enclosed in double quotes from the line pointed at
 * by ptr, and store them quoteless in the passed storage place. */
817
void grab_and_store(const char *kind, char *ptr, regexlisttype **storage)
818
{
819
    regexlisttype *lastthing;
820

821
    if (!opensyntax) {
822
	rcfile_error(
823
		N_("A '%s' command requires a preceding 'syntax' command"), kind);
824
825
826
	return;
    }

827
828
    /* The default syntax doesn't take any file matching stuff. */
    if (strcmp(live_syntax->name, "default") == 0 && *ptr != '\0') {
829
	rcfile_error(
830
		N_("The \"default\" syntax does not accept '%s' regexes"), kind);
831
832
833
834
	return;
    }

    if (*ptr == '\0') {
835
	rcfile_error(N_("Missing regex string after '%s' command"), kind);
836
837
838
	return;
    }

839
840
841
842
843
844
    lastthing = *storage;

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

845
    /* Now gather any valid regexes and add them to the linked list. */
846
847
    while (*ptr != '\0') {
	const char *regexstring;
848
	regexlisttype *newthing;
849
850
851
852

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

856
	regexstring = ++ptr;
857
858
	ptr = parse_next_regex(ptr);
	if (ptr == NULL)
859
	    return;
860

861
	/* If the regex string is malformed, skip it. */
862
	if (!nregcomp(regexstring, NANO_REG_EXTENDED | REG_NOSUB))
863
	    continue;
864

865
866
867
868
	/* 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;
869

870
871
872
873
	if (lastthing == NULL)
	    *storage = newthing;
	else
	    lastthing->next = newthing;
874

875
	lastthing = newthing;
876
877
    }
}
878

879
880
/* Parse and store the name given after a linter/formatter command. */
void pick_up_name(const char *kind, char *ptr, char **storage)
881
882
883
{
    assert(ptr != NULL);

884
    if (!opensyntax) {
885
	rcfile_error(
886
		N_("A '%s' command requires a preceding 'syntax' command"), kind);
887
888
889
890
	return;
    }

    if (*ptr == '\0') {
891
	rcfile_error(N_("Missing command after '%s'"), kind);
892
893
894
	return;
    }

895
    free(*storage);
896

897
    /* Allow unsetting the command by using an empty string. */
898
    if (!strcmp(ptr, "\"\""))
899
	*storage = NULL;
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
    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';
    }
918
    else
919
	*storage = mallocstrcpy(NULL, ptr);
920
}
921
#endif /* !DISABLE_COLOR */
922

923
924
/* Verify that the user has not unmapped every shortcut for a
 * function that we consider 'vital' (such as "Exit"). */
925
926
927
928
929
static void check_vitals_mapped(void)
{
    subnfunc *f;
    int v;
#define VITALS 5
930
    void (*vitals[VITALS])(void) = { do_exit, do_exit, do_cancel, do_cancel, do_cancel };
931
932
933
    int inmenus[VITALS] = { MMAIN, MHELP, MWHEREIS, MREPLACE, MGOTOLINE };

    for  (v = 0; v < VITALS; v++) {
934
935
936
937
938
	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 "
939
					"\"%s\".  Exiting.\n"), f->desc);
940
		    fprintf(stderr, _("If needed, use nano with the -I option "
941
942
					"to adjust your nanorc settings.\n"));
		    exit(1);
943
944
945
946
		}
		break;
	    }
	}
947
948
949
    }
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
950
/* Parse the rcfile, once it has been opened successfully at rcstream,
951
952
953
 * 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)
954
{
955
956
    char *buf = NULL;
    ssize_t len;
957
    size_t n = 0;
958
959
960

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
964
	/* Ignore the newline. */
965
966
	if (buf[len - 1] == '\n')
	    buf[len - 1] = '\0';
967
968
969

	lineno++;
	ptr = buf;
970
	while (isblank((unsigned char)*ptr))
971
972
	    ptr++;

973
974
975
	/* If we have a blank line or a comment, skip to the next
	 * line. */
	if (*ptr == '\0' || *ptr == '#')
976
977
	    continue;

978
	/* Otherwise, skip to the next space. */
979
980
981
	keyword = ptr;
	ptr = parse_next_word(ptr);

982
#ifndef DISABLE_COLOR
983
984
	/* Handle extending first... */
	if (strcasecmp(keyword, "extendsyntax") == 0) {
985
	    syntaxtype *sint;
986
987
988
	    char *syntaxname = ptr;

	    ptr = parse_next_word(ptr);
989
990
991

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

994
995
996
	    if (sint == NULL) {
		rcfile_error(N_("Could not find syntax \"%s\" to extend"),
				syntaxname);
997
		opensyntax = FALSE;
998
999
		continue;
	    }
1000
1001
1002
1003

	    live_syntax = sint;
	    opensyntax = TRUE;

1004
	    /* Refind the tail of the color list for this syntax. */
1005
1006
1007
1008
	    lastcolor = sint->color;
	    if (lastcolor != NULL)
		while (lastcolor->next != NULL)
		    lastcolor = lastcolor->next;
1009

1010
1011
	    keyword = ptr;
	    ptr = parse_next_word(ptr);
1012
1013
	}

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

1065
#ifndef DISABLE_COLOR
1066
1067
	/* If a syntax was extended, it stops at the end of the command. */
	if (live_syntax != syntaxes)
1068
	    opensyntax = FALSE;
1069
1070
#endif

1071
1072
1073
1074
	if (set == 0)
	    continue;

	if (*ptr == '\0') {
1075
	    rcfile_error(N_("Missing option"));
1076
1077
1078
1079
1080
1081
	    continue;
	}

	option = ptr;
	ptr = parse_next_word(ptr);

1082
	/* Find the just read name among the existing options. */
1083
	for (i = 0; rcopts[i].name != NULL; i++) {
1084
1085
1086
1087
1088
1089
1090
1091
1092
	    if (strcasecmp(option, rcopts[i].name) == 0)
		break;
	}

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

1093
#ifdef DEBUG
1094
1095
	fprintf(stderr, "    Option name = \"%s\"\n", rcopts[i].name);
	fprintf(stderr, "    Flag = %ld\n", rcopts[i].flag);
1096
#endif
1097
1098
	/* First handle unsetting. */
	if (set == -1) {
1099
	    if (rcopts[i].flag != 0)
1100
		UNSET(rcopts[i].flag);
1101
	    else
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
		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
1115
				rcopts[i].name);
1116
1117
1118
1119
1120
1121
1122
1123
1124
	    continue;
	}

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

	option = mallocstrcpy(NULL, option);
Chris Allegretta's avatar
Chris Allegretta committed
1125
#ifdef DEBUG
1126
	fprintf(stderr, "    Option argument = \"%s\"\n", option);
Chris Allegretta's avatar
Chris Allegretta committed
1127
#endif
1128
1129
	/* Make sure the option argument is a valid multibyte string. */
	if (!is_valid_mbstring(option)) {
1130
	    rcfile_error(N_("Argument is not a valid multibyte string"));
1131
1132
	    continue;
	}
1133

1134
#ifndef DISABLE_COLOR
1135
1136
	if (strcasecmp(rcopts[i].name, "titlecolor") == 0)
	    specified_color_combo[TITLE_BAR] = option;
1137
1138
	else if (strcasecmp(rcopts[i].name, "numbercolor") == 0)
	    specified_color_combo[LINE_NUMBER] = option;
1139
1140
1141
1142
1143
1144
1145
	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
1146
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1147
#ifndef DISABLE_OPERATINGDIR
1148
1149
1150
	if (strcasecmp(rcopts[i].name, "operatingdir") == 0)
	    operating_dir = option;
	else
Chris Allegretta's avatar
Chris Allegretta committed
1151
#endif
1152
#ifndef DISABLE_WRAPJUSTIFY
1153
1154
1155
1156
1157
	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;
1158
	    } else
1159
		UNSET(NO_WRAP);
1160
	    free(option);
1161
	} else
Chris Allegretta's avatar
Chris Allegretta committed
1162
#endif
1163
#ifndef NANO_TINY
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
	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 +
1180
					whitespace_len[0], NULL, NULL);
1181
1182
	    }
	} else
1183
#endif
1184
#ifndef DISABLE_JUSTIFY
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
	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
1202
#endif
1203
#ifndef NANO_TINY
1204
1205
1206
	if (strcasecmp(rcopts[i].name, "backupdir") == 0)
	    backup_dir = option;
	else
1207
1208
1209
	if (strcasecmp(rcopts[i].name, "wordchars") == 0)
	    word_chars = option;
	else
1210
#endif
1211
#ifndef DISABLE_SPELLER
1212
1213
1214
	if (strcasecmp(rcopts[i].name, "speller") == 0)
	    alt_speller = option;
	else
1215
#endif
1216
1217
1218
1219
1220
	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;
1221
1222
	    }
	    free(option);
1223
1224
	} else
	    assert(FALSE);
1225
    }
1226

1227
#ifndef DISABLE_COLOR
1228
    if (opensyntax && lastcolor == NULL)
1229
	rcfile_error(N_("Syntax \"%s\" has no color commands"),
1230
			live_syntax->name);
1231

1232
    opensyntax = FALSE;
1233
#endif
1234

Chris Allegretta's avatar
Chris Allegretta committed
1235
    free(buf);
1236
1237
    fclose(rcstream);
    lineno = 0;
1238

1239
1240
1241
    return;
}

1242
1243
/* Read and interpret one of the two nanorc files. */
void parse_one_nanorc(void)
1244
1245
1246
{
    FILE *rcstream;

1247
    /* Don't try to open directories nor devices. */
1248
    if (!is_good_file(nanorc))
1249
	return;
1250

1251
#ifdef DEBUG
1252
    fprintf(stderr, "Going to parse file \"%s\"\n", nanorc);
1253
1254
#endif

1255
    rcstream = fopen(nanorc, "rb");
1256
1257
1258

    /* If opening the file succeeded, parse it.  Otherwise, only
     * complain if the file actually exists. */
1259
    if (rcstream != NULL)
1260
	parse_rcfile(rcstream, FALSE);
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
    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();
1272

1273
1274
    /* When configured with --disable-wrapping-as-root, turn wrapping off
     * for root, so that only root's .nanorc or --fill can turn it on. */
1275
#ifdef DISABLE_ROOTWRAPPING
1276
1277
1278
    if (geteuid() == NANO_ROOT_UID)
	SET(NO_WRAP);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1279

1280
    get_homedir();
1281

1282
    if (homedir == NULL)
1283
	rcfile_error(N_("I can't find my home directory!  Wah!"));
1284
    else {
1285
1286
	nanorc = charealloc(nanorc, strlen(homedir) + strlen(RCFILE_NAME) + 2);
	sprintf(nanorc, "%s/%s", homedir, RCFILE_NAME);
1287

1288
1289
	/* Process the current user's nanorc. */
	parse_one_nanorc();
1290
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1291

1292
1293
    check_vitals_mapped();

1294
    free(nanorc);
1295

1296
    if (errors && !ISSET(QUIET) && !ISSET(NO_PAUSES)) {
1297
	errors = FALSE;
1298
	fprintf(stderr, _("\nPress Enter to continue starting nano.\n"));
1299
1300
1301
	while (getchar() != '\n')
	    ;
    }
1302
1303
}

1304
#endif /* !DISABLE_NANORC */