rcfile.c 34.6 KB
Newer Older
1
/**************************************************************************
2
 *   rcfile.c  --  This file is part of GNU nano.                         *
3
 *                                                                        *
4
 *   Copyright (C) 2001-2011, 2013-2017 Free Software Foundation, Inc.    *
5
6
7
 *   Copyright (C) 2014 Mike Frysinger                                    *
 *   Copyright (C) 2014, 2015, 2016 Benno Schulenberg                     *
 *                                                                        *
8
9
10
11
 *   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.                               *
12
 *                                                                        *
13
14
15
16
 *   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.                 *
17
18
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
19
 *   along with this program.  If not, see http://www.gnu.org/licenses/.  *
20
21
22
 *                                                                        *
 **************************************************************************/

23
#include "proto.h"
24

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

33
#ifdef ENABLE_NANORC
34

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
39
static const rcoption rcopts[] = {
40
    {"boldtext", BOLD_TEXT},
41
42
43
#ifdef ENABLE_LINENUMBERS
    {"linenumbers", LINE_NUMBERS},
#endif
44
45
#ifndef DISABLE_JUSTIFY
    {"brackets", 0},
46
#endif
47
48
    {"const", CONSTANT_SHOW},  /* deprecated form, remove in 2018 */
    {"constantshow", CONSTANT_SHOW},
Chris Allegretta's avatar
Chris Allegretta committed
49
#ifndef DISABLE_WRAPJUSTIFY
50
    {"fill", 0},
Chris Allegretta's avatar
Chris Allegretta committed
51
#endif
52
53
#ifndef DISABLE_HISTORIES
    {"historylog", HISTORYLOG},
54
#endif
55
    {"morespace", MORE_SPACE},
56
#ifdef ENABLE_MOUSE
57
    {"mouse", USE_MOUSE},
58
#endif
59
#ifdef ENABLE_MULTIBUFFER
60
61
62
    {"multibuffer", MULTIBUFFER},
#endif
    {"nohelp", NO_HELP},
63
    {"nonewlines", NO_NEWLINES},
64
    {"nopauses", NO_PAUSES},
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
    {"quickblank", QUICK_BLANK},
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
    {"quiet", QUIET},
104
    {"showcursor", SHOW_CURSOR},
105
106
    {"smarthome", SMART_HOME},
    {"smooth", SMOOTH_SCROLL},
107
    {"softwrap", SOFTWRAP},
108
    {"tabstospaces", TABS_TO_SPACES},
109
    {"unix", MAKE_IT_UNIX},
110
    {"whitespace", 0},
111
    {"wordbounds", WORD_BOUNDS},
112
    {"wordchars", 0},
113
114
115
#endif
#ifndef DISABLE_COLOR
    {"titlecolor", 0},
116
    {"numbercolor", 0},
117
118
119
    {"statuscolor", 0},
    {"keycolor", 0},
    {"functioncolor", 0},
120
#endif
121
    {NULL, 0}
122
};
123

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

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

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

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

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

    fprintf(stderr, "\n");
161
}
162
#endif /* ENABLE_NANORC */
163

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

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

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

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

    return ptr;
}
184
#endif /* ENABLE_NANORC || !DISABLE_HISTORIES */
185

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

    assert(ptr != NULL);

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

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

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

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

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

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

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

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

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

    return ptr;
}

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

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

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

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

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

281
282
    opensyntax = FALSE;

283
    assert(ptr != NULL);
284

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

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

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

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

310
    /* Initialize a new syntax struct. */
311
312
313
314
315
316
317
    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;
318
319
320
#ifdef ENABLE_COMMENT
    live_syntax->comment = mallocstrcpy(NULL, GENERAL_COMMENT_CHARACTER);
#else
321
    live_syntax->comment = NULL;
322
#endif
323
    live_syntax->color = NULL;
324
    lastcolor = NULL;
325
    live_syntax->nmultis = 0;
326
327

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

331
332
    opensyntax = TRUE;

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

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

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

350
351
352
353
354
355
/* 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 ||
356
	func == do_home_void || func == do_end_void ||
357
#ifndef NANO_TINY
358
	func == do_prev_word_void || func == do_next_word_void ||
359
#endif
360
361
362
363
364
365
366
367
	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;
}

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

    assert(ptr != NULL);

377
378
379
380
#ifdef DEBUG
    fprintf(stderr, "Starting the rebinding code...\n");
#endif

381
382
383
384
385
386
387
388
    if (*ptr == '\0') {
	rcfile_error(N_("Missing key name"));
	return;
    }

    keyptr = ptr;
    ptr = parse_next_word(ptr);
    keycopy = mallocstrcpy(NULL, keyptr);
389
390
391

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

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

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

424
425
426
    if (dobind) {
	funcptr = ptr;
	ptr = parse_next_word(ptr);
427

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

    menuptr = ptr;
    ptr = parse_next_word(ptr);

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

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

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

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

465
    if (dobind) {
466
467
468
469
470
471
472
473
	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;

474
#ifndef NANO_TINY
475
476
477
	/* Handle the special case of the toggles. */
	if (newsc->scfunc == do_toggle_void)
	    mask = MMAIN;
478
#endif
479

480
481
482
483
484
485
486
487
488
	/* 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
489
	    goto free_copy;
490
491
	}

492
	newsc->menus = menu;
493
	assign_keyinfo(newsc, keycopy, 0);
494

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

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

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

  free_copy:
    free(keycopy);
535
536
}

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/* 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;
}

552
#ifndef DISABLE_COLOR
553
554
/* Read and parse one included syntax file. */
static void parse_one_include(char *file)
555
556
557
558
{
    FILE *rcstream;

    /* Don't open directories, character files, or block files. */
559
    if (!is_good_file(file))
560
	return;
561

562
563
564
565
566
    /* Open the included syntax file. */
    rcstream = fopen(file, "rb");

    if (rcstream == NULL) {
	rcfile_error(_("Error reading %s: %s"), file, strerror(errno));
567
	return;
568
569
    }

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

575
#ifdef DEBUG
576
    fprintf(stderr, "Parsing file \"%s\"\n", file);
577
578
#endif

579
    parse_rcfile(rcstream, TRUE);
580
581
}

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

603
604
605
    globfree(&files);
    free(expanded);

606
    /* We're done with the included file(s).  Restore the original
607
608
609
610
611
     * filename and line number position. */
    nanorc = nanorc_save;
    lineno = lineno_save;
}

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

    rcfile_error(N_("Color \"%s\" not understood.\n"
641
642
643
644
		"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
645
    return -1;
646
647
}

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

657
    assert(ptr != NULL);
658

659
    if (!opensyntax) {
660
	rcfile_error(
661
662
		N_("A '%s' command requires a preceding 'syntax' command"),
		"color");
663
664
665
	return;
    }

666
    if (*ptr == '\0') {
667
	rcfile_error(N_("Missing color name"));
668
	return;
669
670
    }

671
    item = ptr;
672
    ptr = parse_next_word(ptr);
673
    if (!parse_color_names(item, &fg, &bg, &bright))
674
	return;
675

676
    if (*ptr == '\0') {
677
	rcfile_error(N_("Missing regex string after '%s' command"), "color");
678
679
680
	return;
    }

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

691
	if (strncasecmp(ptr, "start=", 6) == 0) {
692
	    ptr += 6;
693
	    expectend = TRUE;
694
695
696
	}

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

703
	item = ++ptr;
704
	ptr = parse_next_regex(ptr);
705
706
707
	if (ptr == NULL)
	    break;

708
	if (*item == '\0') {
709
710
711
	    rcfile_error(N_("Empty regex string"));
	    goodstart = FALSE;
	} else
712
	    goodstart = nregcomp(item, rex_flags);
Benno Schulenberg's avatar
Benno Schulenberg committed
713
714
715
716

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

719
720
721
	    newcolor->fg = fg;
	    newcolor->bg = bg;
	    newcolor->bright = bright;
722
	    newcolor->rex_flags = rex_flags;
723

724
	    newcolor->start_regex = mallocstrcpy(NULL, item);
725
726
	    newcolor->start = NULL;

727
	    newcolor->end_regex = NULL;
728
	    newcolor->end = NULL;
729

730
	    newcolor->next = NULL;
731

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

740
	    lastcolor = newcolor;
Benno Schulenberg's avatar
Benno Schulenberg committed
741
	}
Chris Allegretta's avatar
Chris Allegretta committed
742

743
744
	if (!expectend)
	    continue;
745

746
747
748
749
	if (ptr == NULL || strncasecmp(ptr, "end=", 4) != 0) {
	    rcfile_error(N_("\"start=\" requires a corresponding \"end=\""));
	    return;
	}
750

751
752
753
754
755
	ptr += 4;
	if (*ptr != '"') {
	    rcfile_error(N_("Regex strings must begin and end with a \" character"));
	    continue;
	}
756

757
	item = ++ptr;
758
759
	ptr = parse_next_regex(ptr);
	if (ptr == NULL)
760
	    break;
761

762
	if (*item == '\0') {
763
764
765
766
	    rcfile_error(N_("Empty regex string"));
	    continue;
	}

767
768
	/* If the start regex was invalid, skip past the end regex
	 * to stay in sync. */
Benno Schulenberg's avatar
Benno Schulenberg committed
769
	if (!goodstart)
770
771
772
	    continue;

	/* If it's valid, save the ending regex string. */
773
774
	if (nregcomp(item, rex_flags))
	    newcolor->end_regex = mallocstrcpy(NULL, item);
775
776
777
778

	/* Lame way to skip another static counter. */
	newcolor->id = live_syntax->nmultis;
	live_syntax->nmultis++;
779
    }
780
}
781

782
783
784
785
786
787
/* 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)
788
	return FALSE;
789
790
791
792
793
794
795
796
797
798
799
800
801

    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);
802
	    return FALSE;
803
804
805
806
807
808
809
810
811
812
	}
	*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)
813
	    return FALSE;
814
815
816
    } else
	*fg = -1;

817
    return TRUE;
818
819
}

820
821
/* Read regex strings enclosed in double quotes from the line pointed at
 * by ptr, and store them quoteless in the passed storage place. */
822
void grab_and_store(const char *kind, char *ptr, regexlisttype **storage)
823
{
824
    regexlisttype *lastthing;
825

826
    if (!opensyntax) {
827
	rcfile_error(
828
		N_("A '%s' command requires a preceding 'syntax' command"), kind);
829
830
831
	return;
    }

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

    if (*ptr == '\0') {
840
	rcfile_error(N_("Missing regex string after '%s' command"), kind);
841
842
843
	return;
    }

844
845
846
847
848
849
    lastthing = *storage;

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

850
    /* Now gather any valid regexes and add them to the linked list. */
851
852
    while (*ptr != '\0') {
	const char *regexstring;
853
	regexlisttype *newthing;
854
855
856
857

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

861
	regexstring = ++ptr;
862
863
	ptr = parse_next_regex(ptr);
	if (ptr == NULL)
864
	    return;
865

866
	/* If the regex string is malformed, skip it. */
867
	if (!nregcomp(regexstring, NANO_REG_EXTENDED | REG_NOSUB))
868
	    continue;
869

870
871
872
873
	/* 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;
874

875
876
877
878
	if (lastthing == NULL)
	    *storage = newthing;
	else
	    lastthing->next = newthing;
879

880
	lastthing = newthing;
881
882
    }
}
883

884
885
/* Parse and store the name given after a linter/formatter command. */
void pick_up_name(const char *kind, char *ptr, char **storage)
886
887
888
{
    assert(ptr != NULL);

889
    if (!opensyntax) {
890
	rcfile_error(
891
		N_("A '%s' command requires a preceding 'syntax' command"), kind);
892
893
894
895
	return;
    }

    if (*ptr == '\0') {
896
	rcfile_error(N_("Missing command after '%s'"), kind);
897
898
899
	return;
    }

900
    free(*storage);
901

902
    /* Allow unsetting the command by using an empty string. */
903
    if (!strcmp(ptr, "\"\""))
904
	*storage = NULL;
905
    else if (*ptr == '"') {
906
	char *look, *take;
907

908
	look = take = *storage = mallocstrcpy(NULL, ++ptr);
909

910
	/* Snip out the backslashes of escaped characters. */
911
912
	while (*look != '"') {
	    if (*look == '\0') {
913
914
915
916
		rcfile_error(N_("Argument of '%s' lacks closing \""), kind);
		free(*storage);
		*storage = NULL;
		return;
917
918
	    } else if (*look == '\\' && *(look + 1) != '\0') {
		look++;
919
	    }
920
	    *take++ = *look++;
921
	}
922
	*take = '\0';
923
    }
924
    else
925
	*storage = mallocstrcpy(NULL, ptr);
926
}
927
#endif /* !DISABLE_COLOR */
928

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

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
956
/* Parse the rcfile, once it has been opened successfully at rcstream,
957
958
959
 * 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)
960
{
961
962
    char *buf = NULL;
    ssize_t len;
963
    size_t n = 0;
964
965
966

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
970
	/* Ignore the newline. */
971
972
	if (buf[len - 1] == '\n')
	    buf[len - 1] = '\0';
973
974
975

	lineno++;
	ptr = buf;
976
	while (isblank((unsigned char)*ptr))
977
978
	    ptr++;

979
980
981
	/* If we have a blank line or a comment, skip to the next
	 * line. */
	if (*ptr == '\0' || *ptr == '#')
982
983
	    continue;

984
	/* Otherwise, skip to the next space. */
985
986
987
	keyword = ptr;
	ptr = parse_next_word(ptr);

988
#ifndef DISABLE_COLOR
989
990
	/* Handle extending first... */
	if (strcasecmp(keyword, "extendsyntax") == 0) {
991
	    syntaxtype *sint;
992
993
994
	    char *syntaxname = ptr;

	    ptr = parse_next_word(ptr);
995
996
997

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

1000
1001
1002
	    if (sint == NULL) {
		rcfile_error(N_("Could not find syntax \"%s\" to extend"),
				syntaxname);
1003
		opensyntax = FALSE;
1004
1005
		continue;
	    }
1006
1007
1008
1009

	    live_syntax = sint;
	    opensyntax = TRUE;

1010
	    /* Refind the tail of the color list for this syntax. */
1011
1012
1013
1014
	    lastcolor = sint->color;
	    if (lastcolor != NULL)
		while (lastcolor->next != NULL)
		    lastcolor = lastcolor->next;
1015

1016
1017
	    keyword = ptr;
	    ptr = parse_next_word(ptr);
1018
1019
	}

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

1071
#ifndef DISABLE_COLOR
1072
1073
	/* If a syntax was extended, it stops at the end of the command. */
	if (live_syntax != syntaxes)
1074
	    opensyntax = FALSE;
1075
1076
#endif

1077
1078
1079
1080
	if (set == 0)
	    continue;

	if (*ptr == '\0') {
1081
	    rcfile_error(N_("Missing option"));
1082
1083
1084
1085
1086
1087
	    continue;
	}

	option = ptr;
	ptr = parse_next_word(ptr);

1088
	/* Find the just read name among the existing options. */
1089
	for (i = 0; rcopts[i].name != NULL; i++) {
1090
1091
1092
1093
1094
1095
1096
1097
1098
	    if (strcasecmp(option, rcopts[i].name) == 0)
		break;
	}

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

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

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

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

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

1233
#ifndef DISABLE_COLOR
1234
    if (opensyntax && lastcolor == NULL)
1235
	rcfile_error(N_("Syntax \"%s\" has no color commands"),
1236
			live_syntax->name);
1237

1238
    opensyntax = FALSE;
1239
#endif
1240

Chris Allegretta's avatar
Chris Allegretta committed
1241
    free(buf);
1242
1243
    fclose(rcstream);
    lineno = 0;
1244

1245
1246
1247
    return;
}

1248
1249
/* Read and interpret one of the two nanorc files. */
void parse_one_nanorc(void)
1250
1251
1252
{
    FILE *rcstream;

1253
    /* Don't try to open directories nor devices. */
1254
    if (!is_good_file(nanorc))
1255
	return;
1256

1257
#ifdef DEBUG
1258
    fprintf(stderr, "Going to parse file \"%s\"\n", nanorc);
1259
1260
#endif

1261
    rcstream = fopen(nanorc, "rb");
1262
1263
1264

    /* If opening the file succeeded, parse it.  Otherwise, only
     * complain if the file actually exists. */
1265
    if (rcstream != NULL)
1266
	parse_rcfile(rcstream, FALSE);
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
    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();
1278

1279
1280
    /* When configured with --disable-wrapping-as-root, turn wrapping off
     * for root, so that only root's .nanorc or --fill can turn it on. */
1281
#ifdef DISABLE_ROOTWRAPPING
1282
1283
1284
    if (geteuid() == NANO_ROOT_UID)
	SET(NO_WRAP);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1285

1286
    get_homedir();
1287

1288
    if (homedir == NULL)
1289
	rcfile_error(N_("I can't find my home directory!  Wah!"));
1290
    else {
1291
1292
	nanorc = charealloc(nanorc, strlen(homedir) + strlen(RCFILE_NAME) + 2);
	sprintf(nanorc, "%s/%s", homedir, RCFILE_NAME);
1293

1294
1295
	/* Process the current user's nanorc. */
	parse_one_nanorc();
1296
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1297

1298
1299
    check_vitals_mapped();

1300
    free(nanorc);
1301

1302
    if (errors && !ISSET(QUIET) && !ISSET(NO_PAUSES)) {
1303
	errors = FALSE;
1304
	fprintf(stderr, _("\nPress Enter to continue starting nano.\n"));
1305
1306
1307
	while (getchar() != '\n')
	    ;
    }
1308
1309
}

1310
#endif /* ENABLE_NANORC */