"src/rcfile.c" did not exist on "64f49c7a99f1a5f5b818f86b637e2e8e40f3cbd7"
rcfile.c 33 KB
Newer Older
1
2
/* $Id$ */
/**************************************************************************
3
 *   rcfile.c                                                             *
4
 *                                                                        *
5
 *   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009   *
6
 *   Free Software Foundation, Inc.                                       *
7
8
 *   This program is free software; you can redistribute it and/or modify *
 *   it under the terms of the GNU General Public License as published by *
9
 *   the Free Software Foundation; either version 3, or (at your option)  *
10
11
 *   any later version.                                                   *
 *                                                                        *
12
13
14
15
 *   This program 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.                             *
16
17
18
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
19
20
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA            *
 *   02110-1301, USA.                                                     *
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
35

#ifdef ENABLE_NANORC

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
36
static const rcoption rcopts[] = {
37
    {"boldtext", BOLD_TEXT},
38
39
#ifndef DISABLE_JUSTIFY
    {"brackets", 0},
40
#endif
41
    {"const", CONST_UPDATE},
Chris Allegretta's avatar
Chris Allegretta committed
42
#ifndef DISABLE_WRAPJUSTIFY
43
    {"fill", 0},
Chris Allegretta's avatar
Chris Allegretta committed
44
#endif
45
46
47
#ifndef NANO_TINY
    {"locking", LOCKING},
#endif
48
#ifndef DISABLE_MOUSE
49
    {"mouse", USE_MOUSE},
50
#endif
51
#ifndef DISABLE_MULTIBUFFER
52
53
    {"multibuffer", MULTIBUFFER},
#endif
54
    {"morespace", MORE_SPACE},
55
    {"nofollow", NOFOLLOW_SYMLINKS},
56
    {"nohelp", NO_HELP},
57
    {"nonewlines", NO_NEWLINES},
Chris Allegretta's avatar
Chris Allegretta committed
58
#ifndef DISABLE_WRAPPING
59
    {"nowrap", NO_WRAP},
Chris Allegretta's avatar
Chris Allegretta committed
60
#endif
61
#ifndef DISABLE_OPERATINGDIR
62
    {"operatingdir", 0},
63
#endif
Chris Allegretta's avatar
Chris Allegretta committed
64
    {"preserve", PRESERVE},
65
#ifndef DISABLE_JUSTIFY
66
    {"punct", 0},
67
68
    {"quotestr", 0},
#endif
69
    {"rebinddelete", REBIND_DELETE},
70
    {"rebindkeypad", REBIND_KEYPAD},
71
72
73
#ifdef HAVE_REGEX_H
    {"regexp", USE_REGEXP},
#endif
74
#ifndef DISABLE_SPELLER
75
    {"speller", 0},
76
77
78
#endif
    {"suspend", SUSPEND},
    {"tabsize", 0},
79
    {"tempfile", TEMP_FILE},
80
    {"view", VIEW_MODE},
81
#ifndef NANO_TINY
82
83
    {"autoindent", AUTOINDENT},
    {"backup", BACKUP_FILE},
84
    {"allow_insecure_backup", INSECURE_BACKUP},
85
86
87
88
89
    {"backupdir", 0},
    {"backwards", BACKWARDS_SEARCH},
    {"casesensitive", CASE_SENSITIVE},
    {"cut", CUT_TO_END},
    {"historylog", HISTORYLOG},
90
    {"matchbrackets", 0},
91
    {"noconvert", NO_CONVERT},
92
    {"poslog", POS_HISTORY},
93
    {"quiet", QUIET},
94
    {"quickblank", QUICK_BLANK},
95
96
97
    {"smarthome", SMART_HOME},
    {"smooth", SMOOTH_SCROLL},
    {"tabstospaces", TABS_TO_SPACES},
98
    {"undo", UNDOABLE},
99
    {"whitespace", 0},
100
    {"wordbounds", WORD_BOUNDS},
101
    {"softwrap", SOFTWRAP},
102
#endif
103
    {NULL, 0}
104
};
105

106
static bool errors = FALSE;
107
	/* Whether we got any errors while parsing an rcfile. */
108
static size_t lineno = 0;
109
	/* If we did, the line number where the last error occurred. */
110
static char *nanorc = NULL;
111
	/* The path to the rcfile we're parsing. */
112
#ifndef DISABLE_COLOR
113
114
static syntaxtype *endsyntax = NULL;
	/* The end of the list of syntaxes. */
115
static exttype *endheader = NULL;
116
	/* End of header list. */
117
118
119
static colortype *endcolor = NULL;
	/* The end of the color list for the current syntax. */
#endif
120

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
121
122
123
/* 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
124
void rcfile_error(const char *msg, ...)
125
126
127
{
    va_list ap;

128
129
130
    if (ISSET(QUIET))
	return;

131
    fprintf(stderr, "\n");
132
133
    if (lineno > 0) {
	errors = TRUE;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
134
	fprintf(stderr, _("Error in %s on line %lu: "), nanorc, (unsigned long)lineno);
135
    }
Chris Allegretta's avatar
Chris Allegretta committed
136

137
    va_start(ap, msg);
138
    vfprintf(stderr, _(msg), ap);
139
    va_end(ap);
140
141

    fprintf(stderr, "\n");
142
143
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
144
145
146
/* 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
147
char *parse_next_word(char *ptr)
148
{
149
    while (!isblank(*ptr) && *ptr != '\0')
150
151
152
	ptr++;

    if (*ptr == '\0')
153
	return ptr;
154

155
156
    /* Null-terminate and advance ptr. */
    *ptr++ = '\0';
157

158
    while (isblank(*ptr))
159
160
161
162
	ptr++;

    return ptr;
}
163

164
165
166
167
/* 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
168
169
char *parse_argument(char *ptr)
{
170
    const char *ptr_save = ptr;
Chris Allegretta's avatar
Chris Allegretta committed
171
172
173
174
175
176
177
178
179
180
181
    char *last_quote = NULL;

    assert(ptr != NULL);

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

    do {
	ptr++;
	if (*ptr == '"')
	    last_quote = ptr;
182
    } while (*ptr != '\0');
Chris Allegretta's avatar
Chris Allegretta committed
183
184
185
186
187
188

    if (last_quote == NULL) {
	if (*ptr == '\0')
	    ptr = NULL;
	else
	    *ptr++ = '\0';
189
	rcfile_error(N_("Argument '%s' has an unterminated \""), ptr_save);
Chris Allegretta's avatar
Chris Allegretta committed
190
191
192
193
194
    } else {
	*last_quote = '\0';
	ptr = last_quote + 1;
    }
    if (ptr != NULL)
195
	while (isblank(*ptr))
Chris Allegretta's avatar
Chris Allegretta committed
196
197
198
199
	    ptr++;
    return ptr;
}

200
#ifndef DISABLE_COLOR
201
/* Parse the next regex string from the line at ptr, and return it. */
202
203
char *parse_next_regex(char *ptr)
{
204
205
206
207
    assert(ptr != NULL);

    /* Continue until the end of the line, or a " followed by a space, a
     * blank character, or \0. */
208
    while ((*ptr != '"' || (!isblank(*(ptr + 1)) &&
209
	*(ptr + 1) != '\0')) && *ptr != '\0')
210
211
	ptr++;

212
213
214
    assert(*ptr == '"' || *ptr == '\0');

    if (*ptr == '\0') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
215
216
	rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
217
	return NULL;
218
    }
219

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
220
    /* Null-terminate and advance ptr. */
221
222
    *ptr++ = '\0';

223
    while (isblank(*ptr))
224
225
226
227
228
	ptr++;

    return ptr;
}

229
230
/* Compile the regular expression regex to see if it's valid.  Return
 * TRUE if it is, or FALSE otherwise. */
231
bool nregcomp(const char *regex, int eflags)
232
{
233
    regex_t preg;
234
    const char *r = fixbounds(regex);
235
    int rc = regcomp(&preg, r, REG_EXTENDED | eflags);
236
237

    if (rc != 0) {
238
	size_t len = regerror(rc, &preg, NULL, 0);
239
240
	char *str = charalloc(len);

241
	regerror(rc, &preg, str, len);
242
	rcfile_error(N_("Bad regex \"%s\": %s"), r, str);
243
244
	free(str);
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
245

246
    regfree(&preg);
247
    return (rc == 0);
248
249
}

250
251
/* Parse the next syntax string from the line at ptr, and add it to the
 * global list of color syntaxes. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
252
void parse_syntax(char *ptr)
253
{
Chris Allegretta's avatar
Chris Allegretta committed
254
    const char *fileregptr = NULL, *nameptr = NULL;
255
    syntaxtype *tmpsyntax, *prev_syntax;
256
257
    exttype *endext = NULL;
	/* The end of the extensions list for this syntax. */
258

259
    assert(ptr != NULL);
260

261
262
    if (*ptr == '\0') {
	rcfile_error(N_("Missing syntax name"));
263
	return;
264
    }
265
266

    if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
267
268
	rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
269
	return;
270
    }
271

272
273
274
275
276
    ptr++;

    nameptr = ptr;
    ptr = parse_next_regex(ptr);

277
    if (ptr == NULL)
278
	return;
279

280
281
    /* Search for a duplicate syntax name.  If we find one, free it, so
     * that we always use the last syntax with a given name. */
282
    prev_syntax = NULL;
283
284
    for (tmpsyntax = syntaxes; tmpsyntax != NULL;
	tmpsyntax = tmpsyntax->next) {
285
	if (strcmp(nameptr, tmpsyntax->desc) == 0) {
286
287
288
289
	    syntaxtype *old_syntax = tmpsyntax;

	    if (endsyntax == tmpsyntax)
		endsyntax = prev_syntax;
290
291

	    tmpsyntax = tmpsyntax->next;
292
293
294
295
296
297
298
	    if (prev_syntax != NULL)
		prev_syntax->next = tmpsyntax;
	    else
		syntaxes = tmpsyntax;

	    free(old_syntax->desc);
	    free(old_syntax);
299
	    break;
300
	}
301
	prev_syntax = tmpsyntax;
302
303
    }

Chris Allegretta's avatar
Chris Allegretta committed
304
305
    if (syntaxes == NULL) {
	syntaxes = (syntaxtype *)nmalloc(sizeof(syntaxtype));
306
	endsyntax = syntaxes;
Chris Allegretta's avatar
Chris Allegretta committed
307
    } else {
308
309
	endsyntax->next = (syntaxtype *)nmalloc(sizeof(syntaxtype));
	endsyntax = endsyntax->next;
310
#ifdef DEBUG
311
	fprintf(stderr, "Adding new syntax after first one\n");
312
#endif
Chris Allegretta's avatar
Chris Allegretta committed
313
    }
314

315
316
317
    endsyntax->desc = mallocstrcpy(NULL, nameptr);
    endsyntax->color = NULL;
    endcolor = NULL;
318
    endheader = NULL;
319
    endsyntax->extensions = NULL;
320
    endsyntax->headers = NULL;
321
    endsyntax->magics = NULL;
322
    endsyntax->next = NULL;
323
    endsyntax->nmultis = 0;
324
    endsyntax->linter = NULL;
325

326
#ifdef DEBUG
327
    fprintf(stderr, "Starting a new syntax type: \"%s\"\n", nameptr);
328
329
#endif

330
331
332
333
334
335
336
    /* The "none" syntax is the same as not having a syntax at all, so
     * we can't assign any extensions or colors to it. */
    if (strcmp(endsyntax->desc, "none") == 0) {
	rcfile_error(N_("The \"none\" syntax is reserved"));
	return;
    }

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

344
345
    /* Now load the extensions into their part of the struct. */
    while (*ptr != '\0') {
346
347
348
	exttype *newext;
	    /* The new extension structure. */

349
	while (*ptr != '"' && *ptr != '\0')
350
351
	    ptr++;

352
	if (*ptr == '\0')
353
	    return;
354

355
356
357
358
	ptr++;

	fileregptr = ptr;
	ptr = parse_next_regex(ptr);
359
360
	if (ptr == NULL)
	    break;
361

362
	newext = (exttype *)nmalloc(sizeof(exttype));
363
364
365
366
367
368

	/* Save the extension regex if it's valid. */
	if (nregcomp(fileregptr, REG_NOSUB)) {
	    newext->ext_regex = mallocstrcpy(NULL, fileregptr);
	    newext->ext = NULL;

369
	    if (endext == NULL)
370
		endsyntax->extensions = newext;
371
372
373
374
	    else
		endext->next = newext;
	    endext = newext;
	    endext->next = NULL;
375
376
	} else
	    free(newext);
377
    }
378
379
}

380
/* Parse the magic regex that may influence the choice of syntax. */
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
void parse_magictype(char *ptr)
{
#ifdef HAVE_LIBMAGIC
    const char *fileregptr = NULL;
    exttype *endext = NULL;

    assert(ptr != NULL);

    if (syntaxes == NULL) {
	rcfile_error(
		N_("Cannot add a magic string regex without a syntax command"));
	return;
    }

    if (*ptr == '\0') {
	rcfile_error(N_("Missing magic string name"));
	return;
    }

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

#ifdef DEBUG
    fprintf(stderr, "Starting a magic type: \"%s\"\n", ptr);
#endif

    /* Now load the extensions into their part of the struct. */
    while (*ptr != '\0') {
	exttype *newext;
	    /* The new extension structure. */

	while (*ptr != '"' && *ptr != '\0')
	    ptr++;

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

	ptr++;

	fileregptr = ptr;
	ptr = parse_next_regex(ptr);
	if (ptr == NULL)
	    break;

	newext = (exttype *)nmalloc(sizeof(exttype));

	/* Save the regex if it's valid. */
	if (nregcomp(fileregptr, REG_NOSUB)) {
	    newext->ext_regex = mallocstrcpy(NULL, fileregptr);
	    newext->ext = NULL;

	    if (endext == NULL)
		endsyntax->magics = newext;
	    else
		endext->next = newext;
	    endext = newext;
	    endext->next = NULL;
	} else
	    free(newext);
    }
#endif /* HAVE_LIBMAGIC */
445
}
446
#endif /* !DISABLE_COLOR */
447

448

449
450
451
452
453
454
455
456
457
458
459
460
461
462
int check_bad_binding(sc *s)
{
#define BADLISTLEN 1
    int badtypes[BADLISTLEN] = {META};
    int badseqs[BADLISTLEN] = { 91 };
    int i;

    for (i = 0; i < BADLISTLEN; i++)
        if (s->type == badtypes[i] && s->seq == badseqs[i])
	    return 1;

    return 0;
}

463
/* Bind or unbind a key combo, to or from a function. */
464
void parse_binding(char *ptr, bool dobind)
465
466
{
    char *keyptr = NULL, *keycopy = NULL, *funcptr = NULL, *menuptr = NULL;
467
    sc *s, *newsc = NULL;
468
    int menu;
469
470
471

    assert(ptr != NULL);

472
473
474
475
#ifdef DEBUG
    fprintf(stderr, "Starting the rebinding code...\n");
#endif

476
477
478
479
480
481
482
483
    if (*ptr == '\0') {
	rcfile_error(N_("Missing key name"));
	return;
    }

    keyptr = ptr;
    ptr = parse_next_word(ptr);
    keycopy = mallocstrcpy(NULL, keyptr);
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

    if (strlen(keycopy) < 2) {
	rcfile_error(N_("Key name is too short"));
	return;
    }

    /* 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"));
	    return;
	}
    }
501

502
    if (keycopy[0] != 'M' && keycopy[0] != '^' && keycopy[0] != 'F' && keycopy[0] != 'K') {
503
	rcfile_error(N_("Key name must begin with \"^\", \"M\", or \"F\""));
504
505
506
	return;
    }

507
508
509
    if (dobind) {
	funcptr = ptr;
	ptr = parse_next_word(ptr);
510

511
512
513
514
	if (!strcmp(funcptr, "")) {
	    rcfile_error(N_("Must specify function to bind key to"));
	    return;
	}
515
516
517
518
519
    }

    menuptr = ptr;
    ptr = parse_next_word(ptr);

520
    if (!strcmp(menuptr, "")) {
521
	/* TRANSLATORS: Do not translate the word "all". */
522
	rcfile_error(N_("Must specify menu in which to bind/unbind key (or \"all\")"));
523
524
525
	return;
    }

526
    menu = strtomenu(menuptr);
527
528
529
530
    if (menu < 1) {
	rcfile_error(N_("Cannot map name \"%s\" to a menu"), menuptr);
	return;
    }
531
532

    if (dobind) {
533
	newsc = strtosc(funcptr);
534
	if (newsc == NULL) {
535
	    rcfile_error(N_("Cannot map name \"%s\" to a function"), funcptr);
536
537
	    return;
	}
538
539
    }

540
#ifdef DEBUG
541
542
543
544
545
    if (dobind)
	fprintf(stderr, "newsc now address %d, func assigned = %d, menu = %x\n",
	    &newsc, newsc->scfunc, menu);
   else
	fprintf(stderr, "unbinding \"%s\" from menu %x\n", keycopy, menu);
546
547
#endif

548
549
550
551
552
    if (dobind) {
	newsc->keystr = keycopy;
	newsc->menu = menu;
	newsc->type = strtokeytype(newsc->keystr);
	assign_keyinfo(newsc);
553
#ifdef DEBUG
554
555
	fprintf(stderr, "s->keystr = \"%s\"\n", newsc->keystr);
	fprintf(stderr, "s->seq = \"%d\"\n", newsc->seq);
556
557
#endif

558
559
560
	if (check_bad_binding(newsc)) {
	    rcfile_error(N_("Sorry, keystr \"%s\" is an illegal binding"), newsc->keystr);
	    return;
561
562
	}
    }
563

564
    /* Now find and delete any existing same shortcut in the menu(s). */
565
    for (s = sclist; s != NULL; s = s->next) {
566
        if (((s->menu & menu)) && !strcmp(s->keystr, keycopy)) {
567
#ifdef DEBUG
568
	    fprintf(stderr, "deleting entry from menu %x\n", s->menu);
569
#endif
570
	    s->menu &= ~menu;
571
572
	}
    }
573
574
575
576
577
578

    if (dobind) {
	/* Add the new shortcut at the start of the list. */
	newsc->next = sclist;
	sclist = newsc;
    }
579
580
}

581

582
#ifndef DISABLE_COLOR
583
/* Read and parse additional syntax files. */
584
static void _parse_include(char *file)
585
586
587
588
{
    struct stat rcinfo;
    FILE *rcstream;

589
590
591
    /* Can't get the specified file's full path because it may screw up
     * our cwd depending on the parent directories' permissions (see
     * Savannah bug #25297). */
592
593

    /* Don't open directories, character files, or block files. */
594
    if (stat(file, &rcinfo) != -1) {
595
596
597
598
	if (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") :
599
		_("\"%s\" is a device file"), file);
600
601
602
603
	}
    }

    /* Open the new syntax file. */
604
605
    if ((rcstream = fopen(file, "rb")) == NULL) {
	rcfile_error(_("Error reading %s: %s"), file,
606
		strerror(errno));
607
	return;
608
609
610
611
    }

    /* Use the name and line number position of the new syntax file
     * while parsing it, so we can know where any errors in it are. */
612
    nanorc = file;
613
614
    lineno = 0;

615
#ifdef DEBUG
616
    fprintf(stderr, "Parsing file \"%s\"\n", file);
617
618
#endif

619
    parse_rcfile(rcstream
620
#ifndef DISABLE_COLOR
621
622
623
	, TRUE
#endif
	);
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
}

void parse_include(char *ptr)
{
    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)
	    _parse_include(files.gl_pathv[i]);
    } else {
	rcfile_error(_("Error expanding %s: %s"), option,
		strerror(errno));
    }
647
648
649
650
651
652
653

    /* We're done with the new syntax file.  Restore the original
     * filename and line number position. */
    nanorc = nanorc_save;
    lineno = lineno_save;
}

654
655
/* Return the short value corresponding to the color named in colorname,
 * and set bright to TRUE if that color is bright. */
656
short color_to_short(const char *colorname, bool *bright)
657
{
658
    short mcolor = -1;
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692

    assert(colorname != NULL && bright != NULL);

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

    if (strcasecmp(colorname, "green") == 0)
	mcolor = COLOR_GREEN;
    else if (strcasecmp(colorname, "red") == 0)
	mcolor = COLOR_RED;
    else if (strcasecmp(colorname, "blue") == 0)
	mcolor = COLOR_BLUE;
    else if (strcasecmp(colorname, "white") == 0)
	mcolor = COLOR_WHITE;
    else if (strcasecmp(colorname, "yellow") == 0)
	mcolor = COLOR_YELLOW;
    else if (strcasecmp(colorname, "cyan") == 0)
	mcolor = COLOR_CYAN;
    else if (strcasecmp(colorname, "magenta") == 0)
	mcolor = COLOR_MAGENTA;
    else if (strcasecmp(colorname, "black") == 0)
	mcolor = COLOR_BLACK;
    else
	rcfile_error(N_("Color \"%s\" not understood.\n"
		"Valid colors are \"green\", \"red\", \"blue\",\n"
		"\"white\", \"yellow\", \"cyan\", \"magenta\" and\n"
		"\"black\", with the optional prefix \"bright\"\n"
		"for foreground colors."), colorname);

    return mcolor;
}

693
694
695
/* Parse the color string in the line at ptr, and add it to the current
 * file's associated colors.  If icase is TRUE, treat the color string
 * as case insensitive. */
696
void parse_colors(char *ptr, bool icase)
697
{
698
    short fg, bg;
699
    bool bright = FALSE, no_fgcolor = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
700
    char *fgstr;
701

702
    assert(ptr != NULL);
703

704
705
    if (syntaxes == NULL) {
	rcfile_error(
706
		N_("Cannot add a color command without a syntax command"));
707
708
709
	return;
    }

710
    if (*ptr == '\0') {
711
	rcfile_error(N_("Missing color name"));
712
	return;
713
714
    }

715
716
717
718
    fgstr = ptr;
    ptr = parse_next_word(ptr);

    if (strchr(fgstr, ',') != NULL) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
719
	char *bgcolorname;
720

721
	strtok(fgstr, ",");
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
722
	bgcolorname = strtok(NULL, ",");
723
724
725
726
727
728
	if (bgcolorname == NULL) {
	    /* If we have a background color without a foreground color,
	     * parse it properly. */
	    bgcolorname = fgstr + 1;
	    no_fgcolor = TRUE;
	}
729
	if (strncasecmp(bgcolorname, "bright", 6) == 0) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
730
	    rcfile_error(
731
		N_("Background color \"%s\" cannot be bright"),
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
732
		bgcolorname);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
733
734
	    return;
	}
735
	bg = color_to_short(bgcolorname, &bright);
736
    } else
Chris Allegretta's avatar
Chris Allegretta committed
737
	bg = -1;
738

739
    if (!no_fgcolor) {
740
	fg = color_to_short(fgstr, &bright);
741

742
743
744
745
746
	/* Don't try to parse screwed-up foreground colors. */
	if (fg == -1)
	    return;
    } else
	fg = -1;
747

748
    if (*ptr == '\0') {
749
	rcfile_error(N_("Missing regex string"));
750
751
752
	return;
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
753
    /* Now for the fun part.  Start adding regexes to individual strings
754
755
     * in the colorstrings array, woo! */
    while (ptr != NULL && *ptr != '\0') {
756
757
	colortype *newcolor;
	    /* The new color structure. */
758
	bool cancelled = FALSE;
759
	    /* The start expression was bad. */
760
761
	bool expectend = FALSE;
	    /* Do we expect an end= line? */
762

763
	if (strncasecmp(ptr, "start=", 6) == 0) {
764
	    ptr += 6;
765
	    expectend = TRUE;
766
767
768
	}

	if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
769
770
	    rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
771
	    ptr = parse_next_regex(ptr);
772
773
	    continue;
	}
774

775
	ptr++;
776

777
778
	fgstr = ptr;
	ptr = parse_next_regex(ptr);
779
780
781
	if (ptr == NULL)
	    break;

782
	newcolor = (colortype *)nmalloc(sizeof(colortype));
783

784
785
786
	/* Save the starting regex string if it's valid, and set up the
	 * color information. */
	if (nregcomp(fgstr, icase ? REG_ICASE : 0)) {
787
788
789
	    newcolor->fg = fg;
	    newcolor->bg = bg;
	    newcolor->bright = bright;
790
	    newcolor->icase = icase;
791
792
793
794

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

795
	    newcolor->end_regex = NULL;
796
	    newcolor->end = NULL;
797

798
	    newcolor->next = NULL;
799

800
801
	    if (endcolor == NULL) {
		endsyntax->color = newcolor;
802
#ifdef DEBUG
803
		fprintf(stderr, "Starting a new colorstring for fg %hd, bg %hd\n", fg, bg);
804
#endif
805
	    } else {
Chris Allegretta's avatar
Chris Allegretta committed
806
#ifdef DEBUG
807
		fprintf(stderr, "Adding new entry for fg %hd, bg %hd\n", fg, bg);
Chris Allegretta's avatar
Chris Allegretta committed
808
#endif
809
810
		/* Need to recompute endcolor now so we can extend
		 * colors to syntaxes. */
811
		for (endcolor = endsyntax->color; endcolor->next != NULL; endcolor = endcolor->next)
812
		    ;
813
		endcolor->next = newcolor;
814
	    }
815

816
	    endcolor = newcolor;
817
818
819
	} else {
	    free(newcolor);
	    cancelled = TRUE;
820
	}
Chris Allegretta's avatar
Chris Allegretta committed
821

822
	if (expectend) {
823
	    if (ptr == NULL || strncasecmp(ptr, "end=", 4) != 0) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
824
825
		rcfile_error(
			N_("\"start=\" requires a corresponding \"end=\""));
826
827
828
829
		return;
	    }
	    ptr += 4;
	    if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
830
831
		rcfile_error(
			N_("Regex strings must begin and end with a \" character"));
832
		continue;
833
	    }
834

835
836
	    ptr++;

837
	    fgstr = ptr;
838
	    ptr = parse_next_regex(ptr);
839
840
	    if (ptr == NULL)
		break;
841

842
843
	    /* If the start regex was invalid, skip past the end regex
	     * to stay in sync. */
844
845
	    if (cancelled)
		continue;
846

847
848
849
	    /* Save the ending regex string if it's valid. */
	    newcolor->end_regex = (nregcomp(fgstr, icase ? REG_ICASE :
		0)) ? mallocstrcpy(NULL, fgstr) : NULL;
850

851
	    /* Lame way to skip another static counter. */
852
            newcolor->id = endsyntax->nmultis;
853
            endsyntax->nmultis++;
Chris Allegretta's avatar
Chris Allegretta committed
854
	}
855
    }
856
}
857

858
/* Parse the header-line regex that may influence the choice of syntax. */
859
860
void parse_headers(char *ptr)
{
861
    char *regstr;
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918

    assert(ptr != NULL);

    if (syntaxes == NULL) {
	rcfile_error(
		N_("Cannot add a header regex without a syntax command"));
	return;
    }

    if (*ptr == '\0') {
	rcfile_error(N_("Missing regex string"));
	return;
    }

    /* Now for the fun part.  Start adding regexes to individual strings
     * in the colorstrings array, woo! */
    while (ptr != NULL && *ptr != '\0') {
	exttype *newheader;
	    /* The new color structure. */

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

	ptr++;

	regstr = ptr;
	ptr = parse_next_regex(ptr);
	if (ptr == NULL)
	    break;

	newheader = (exttype *)nmalloc(sizeof(exttype));

	/* Save the regex string if it's valid */
	if (nregcomp(regstr, 0)) {
	    newheader->ext_regex = mallocstrcpy(NULL, regstr);
	    newheader->ext = NULL;
	    newheader->next = NULL;

#ifdef DEBUG
	     fprintf(stderr, "Starting a new header entry: %s\n", newheader->ext_regex);
#endif

	    if (endheader == NULL) {
		endsyntax->headers = newheader;
	    } else {
		endheader->next = newheader;
	    }

	    endheader = newheader;
	} else
	    free(newheader);
    }
}
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938

/* Parse the linter requested for this syntax.  Simple? */
void parse_linter(char *ptr)
{
    assert(ptr != NULL);

    if (syntaxes == NULL) {
	rcfile_error(
		N_("Cannot add a linter without a syntax command"));
	return;
    }

    if (*ptr == '\0') {
	rcfile_error(N_("Missing linter command"));
	return;
    }

    if (endsyntax->linter != NULL)
	free(endsyntax->linter);

939
940
941
942
943
    /* Let them unset the linter by using "" */
    if (!strcmp(ptr, "\"\""))
	endsyntax->linter = NULL;
    else
	endsyntax->linter = mallocstrcpy(syntaxes->linter, ptr);
944
}
945
#endif /* !DISABLE_COLOR */
946

947
/* Check whether the user has unmapped every shortcut for a
948
 * sequence we consider 'vital', like the exit function. */
949
950
951
952
953
static void check_vitals_mapped(void)
{
    subnfunc *f;
    int v;
#define VITALS 5
954
    void (*vitals[VITALS])(void) = { do_exit, do_exit, do_cancel, do_cancel, do_cancel };
955
956
957
958
959
960
961
    int inmenus[VITALS] = { MMAIN, MHELP, MWHEREIS, MREPLACE, MGOTOLINE };

    for  (v = 0; v < VITALS; v++) {
       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) {
962
963
964
                   fprintf(stderr, _("Fatal error: no keys mapped for function "
				     "\"%s\".  Exiting.\n"), f->desc);
                   fprintf(stderr, _("If needed, use nano with the -I option "
965
				     "to adjust your nanorc settings.\n"));
966
967
968
969
970
971
972
973
                   exit(1);
               }
           break;
           }
       }
    }
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
974
975
976
/* Parse the rcfile, once it has been opened successfully at rcstream,
 * and close it afterwards.  If syntax_only is TRUE, only allow the file
 * to contain color syntax commands: syntax, color, and icolor. */
977
void parse_rcfile(FILE *rcstream
978
#ifndef DISABLE_COLOR
979
980
981
	, bool syntax_only
#endif
	)
982
{
983
984
    char *buf = NULL;
    ssize_t len;
985
    size_t n = 0;
986
#ifndef DISABLE_COLOR
987
988
    syntaxtype *end_syn_save = NULL;
#endif
989
990
991

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
995
	/* Ignore the newline. */
996
997
	if (buf[len - 1] == '\n')
	    buf[len - 1] = '\0';
998
999
1000

	lineno++;
	ptr = buf;
1001
	while (isblank(*ptr))
1002
1003
	    ptr++;

1004
1005
1006
	/* If we have a blank line or a comment, skip to the next
	 * line. */
	if (*ptr == '\0' || *ptr == '#')
1007
1008
	    continue;

1009
	/* Otherwise, skip to the next space. */
1010
1011
1012
	keyword = ptr;
	ptr = parse_next_word(ptr);

1013
#ifndef DISABLE_COLOR
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
	/* Handle extending first... */
	if (strcasecmp(keyword, "extendsyntax") == 0) {
	    char *syntaxname = ptr;
	    syntaxtype *ts = NULL;

	    ptr = parse_next_word(ptr);
	    for (ts = syntaxes; ts != NULL; ts = ts->next)
		if (!strcmp(ts->desc, syntaxname))
		    break;

	    if (ts == NULL) {
		rcfile_error(N_("Could not find syntax \"%s\" to extend"), syntaxname);
		continue;
	    } else {
		end_syn_save = endsyntax;
		endsyntax = ts;
		keyword = ptr;
		ptr = parse_next_word(ptr);
	    }
	}
1034
#endif
1035

1036
	/* Try to parse the keyword. */
1037
	if (strcasecmp(keyword, "set") == 0) {
1038
#ifndef DISABLE_COLOR
1039
1040
	    if (syntax_only)
		rcfile_error(
1041
			N_("Command \"%s\" not allowed in included file"),
1042
1043
1044
1045
1046
			keyword);
	    else
#endif
		set = 1;
	} else if (strcasecmp(keyword, "unset") == 0) {
1047
#ifndef DISABLE_COLOR
1048
1049
	    if (syntax_only)
		rcfile_error(
1050
			N_("Command \"%s\" not allowed in included file"),
1051
1052
1053
1054
1055
			keyword);
	    else
#endif
		set = -1;
	}
1056
#ifndef DISABLE_COLOR
1057
1058
1059
	else if (strcasecmp(keyword, "include") == 0) {
	    if (syntax_only)
		rcfile_error(
1060
			N_("Command \"%s\" not allowed in included file"),
1061
1062
1063
			keyword);
	    else
		parse_include(ptr);
1064
1065
	} else if (strcasecmp(keyword, "syntax") == 0) {
	    if (endsyntax != NULL && endcolor == NULL)
1066
		rcfile_error(N_("Syntax \"%s\" has no color commands"),
1067
			endsyntax->desc);
Chris Allegretta's avatar
Chris Allegretta committed
1068
	    parse_syntax(ptr);
1069
	}
1070
	else if (strcasecmp(keyword, "magic") == 0)
1071
	    parse_magictype(ptr);
1072
	else if (strcasecmp(keyword, "header") == 0)
1073
1074
	    parse_headers(ptr);
	else if (strcasecmp(keyword, "color") == 0)
1075
1076
1077
	    parse_colors(ptr, FALSE);
	else if (strcasecmp(keyword, "icolor") == 0)
	    parse_colors(ptr, TRUE);
1078
1079
	else if (strcasecmp(keyword, "linter") == 0)
	    parse_linter(ptr);
1080
#endif /* !DISABLE_COLOR */
1081
	else if (strcasecmp(keyword, "bind") == 0)
1082
	    parse_binding(ptr, TRUE);
1083
	else if (strcasecmp(keyword, "unbind") == 0)
1084
	    parse_binding(ptr, FALSE);
1085
	else
1086
	    rcfile_error(N_("Command \"%s\" not understood"), keyword);
1087

1088
1089
1090
#ifndef DISABLE_COLOR
	/* If we temporarily reset endsyntax to allow extending,
	 * restore the value here. */
1091
1092
1093
1094
1095
1096
	if (end_syn_save != NULL) {
	    endsyntax = end_syn_save;
	    end_syn_save = NULL;
	}
#endif

1097
1098
1099
1100
	if (set == 0)
	    continue;

	if (*ptr == '\0') {
1101
	    rcfile_error(N_("Missing option"));
1102
1103
1104
1105
1106
1107
	    continue;
	}

	option = ptr;
	ptr = parse_next_word(ptr);

1108
1109
	for (i = 0; rcopts[i].name != NULL; i++) {
	    if (strcasecmp(option, rcopts[i].name) == 0) {
1110
#ifdef DEBUG
1111
1112
1113
1114
1115
1116
1117
1118
1119
		fprintf(stderr, "parse_rcfile(): name = \"%s\"\n", rcopts[i].name);
#endif
		if (set == 1) {
		    if (rcopts[i].flag != 0)
			/* This option has a flag, so it doesn't take an
			 * argument. */
			SET(rcopts[i].flag);
		    else {
			/* This option doesn't have a flag, so it takes
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1120
			 * an argument. */
1121
			if (*ptr == '\0') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1122
			    rcfile_error(
1123
				N_("Option \"%s\" requires an argument"),
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1124
				rcopts[i].name);
1125
1126
1127
1128
1129
1130
			    break;
			}
			option = ptr;
			if (*option == '"')
			    option++;
			ptr = parse_argument(ptr);
1131

1132
			option = mallocstrcpy(NULL, option);
Chris Allegretta's avatar
Chris Allegretta committed
1133
#ifdef DEBUG
1134
			fprintf(stderr, "option = \"%s\"\n", option);
Chris Allegretta's avatar
Chris Allegretta committed
1135
#endif
1136
1137
1138
1139
1140
1141
1142
1143
1144

			/* Make sure option is a valid multibyte
			 * string. */
			if (!is_valid_mbstring(option)) {
			    rcfile_error(
				N_("Option is not a valid multibyte string"));
			    break;
			}

Chris Allegretta's avatar
Chris Allegretta committed
1145
#ifndef DISABLE_OPERATINGDIR
1146
			if (strcasecmp(rcopts[i].name, "operatingdir") == 0)
1147
			    operating_dir = option;
1148
			else
Chris Allegretta's avatar
Chris Allegretta committed
1149
#endif
1150
#ifndef DISABLE_WRAPJUSTIFY
1151
1152
			if (strcasecmp(rcopts[i].name, "fill") == 0) {
			    if (!parse_num(option, &wrap_at)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1153
				rcfile_error(
1154
					N_("Requested fill size \"%s\" is invalid"),
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1155
					option);
1156
				wrap_at = -CHARS_FROM_EOL;
1157
1158
			    } else
				free(option);
1159
			} else
Chris Allegretta's avatar
Chris Allegretta committed
1160
#endif
1161
#ifndef NANO_TINY
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) {
1173
			    whitespace = option;
1174
1175
			    if (mbstrlen(whitespace) != 2 ||
				strlenpt(whitespace) != 2) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1176
1177
				rcfile_error(
					N_("Two single-column characters required"));
1178
1179
				free(whitespace);
				whitespace = NULL;
1180
1181
1182
			    } else {
				whitespace_len[0] =
					parse_mbchar(whitespace, NULL,
1183
					NULL);
1184
1185
				whitespace_len[1] =
					parse_mbchar(whitespace +
1186
					whitespace_len[0], NULL, NULL);
1187
1188
			    }
			} else
1189
#endif
1190
#ifndef DISABLE_JUSTIFY
1191
			if (strcasecmp(rcopts[i].name, "punct") == 0) {
1192
			    punct = option;
1193
			    if (has_blank_mbchars(punct)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1194
				rcfile_error(
1195
					N_("Non-blank characters required"));
1196
1197
1198
				free(punct);
				punct = NULL;
			    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1199
1200
			} else if (strcasecmp(rcopts[i].name,
				"brackets") == 0) {
1201
			    brackets = option;
1202
			    if (has_blank_mbchars(brackets)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1203
				rcfile_error(
1204
					N_("Non-blank characters required"));
1205
1206
1207
				free(brackets);
				brackets = NULL;
			    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1208
1209
			} else if (strcasecmp(rcopts[i].name,
				"quotestr") == 0)
1210
			    quotestr = option;
1211
			else
1212
#endif
1213
#ifndef NANO_TINY
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1214
1215
			if (strcasecmp(rcopts[i].name,
				"backupdir") == 0)
1216
			    backup_dir = option;
1217
			else
1218
#endif
1219
#ifndef DISABLE_SPELLER
1220
			if (strcasecmp(rcopts[i].name, "speller") == 0)
1221
			    alt_speller = option;
1222
1223
			else
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1224
1225
1226
1227
1228
			if (strcasecmp(rcopts[i].name,
				"tabsize") == 0) {
			    if (!parse_num(option, &tabsize) ||
				tabsize <= 0) {
				rcfile_error(
1229
					N_("Requested tab size \"%s\" is invalid"),
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1230
					option);
1231
				tabsize = -1;
1232
1233
			    } else
				free(option);
1234
			} else
1235
1236
			    assert(FALSE);
		    }
1237
#ifdef DEBUG
1238
		    fprintf(stderr, "flag = %ld\n", rcopts[i].flag);
1239
#endif
1240
1241
1242
		} else if (rcopts[i].flag != 0)
		    UNSET(rcopts[i].flag);
		else
1243
		    rcfile_error(N_("Cannot unset option \"%s\""),
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1244
			rcopts[i].name);
1245
		/* If undo/redo was enabled, reinitialize the lists. */
1246
		if (strcasecmp(rcopts[i].name, "undo") == 0)
1247
		    shortcut_init();
1248
		break;
1249
1250
	    }
	}
1251
	if (rcopts[i].name == NULL)
1252
	    rcfile_error(N_("Unknown option \"%s\""), option);
1253
    }
1254

1255
#ifndef DISABLE_COLOR
1256
    if (endsyntax != NULL && endcolor == NULL)
1257
	rcfile_error(N_("Syntax \"%s\" has no color commands"),
1258
		endsyntax->desc);
1259
#endif
1260

Chris Allegretta's avatar
Chris Allegretta committed
1261
    free(buf);
1262
1263
    fclose(rcstream);
    lineno = 0;
1264

Chris Allegretta's avatar
Chris Allegretta committed
1265
    check_vitals_mapped();
1266
1267
1268
    return;
}

1269
/* The main rcfile function.  It tries to open the system-wide rcfile,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1270
 * followed by the current user's rcfile. */
1271
1272
void do_rcfile(void)
{
1273
    struct stat rcinfo;
1274
1275
    FILE *rcstream;

1276
    nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc");
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286

    /* Don't open directories, character files, or block files. */
    if (stat(nanorc, &rcinfo) != -1) {
	if (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"), nanorc);
    }

1287
1288
1289
1290
#ifdef DEBUG
    fprintf(stderr, "Parsing file \"%s\"\n", nanorc);
#endif

1291
    /* Try to open the system-wide nanorc. */
1292
    rcstream = fopen(nanorc, "rb");
1293
    if (rcstream != NULL)
1294
	parse_rcfile(rcstream
1295
#ifndef DISABLE_COLOR
1296
1297
1298
		, FALSE
#endif
		);
1299

1300
#ifdef DISABLE_ROOTWRAPPING
1301
    /* We've already read SYSCONFDIR/nanorc, if it's there.  If we're
1302
1303
     * root, and --disable-wrapping-as-root is used, turn wrapping off
     * now. */
1304
1305
1306
    if (geteuid() == NANO_ROOT_UID)
	SET(NO_WRAP);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1307

1308
    get_homedir();
1309

1310
    if (homedir == NULL)
1311
	rcfile_error(N_("I can't find my home directory!  Wah!"));
1312
    else {
1313
1314
1315
1316
1317
#ifndef RCFILE_NAME
#define RCFILE_NAME ".nanorc"
#endif
	nanorc = charealloc(nanorc, strlen(homedir) + strlen(RCFILE_NAME) + 2);
	sprintf(nanorc, "%s/%s", homedir, RCFILE_NAME);
1318

1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
	/* Don't open directories, character files, or block files. */
	if (stat(nanorc, &rcinfo) != -1) {
	    if (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"), nanorc);
	}

	/* Try to open the current user's nanorc. */
	rcstream = fopen(nanorc, "rb");
1330
	if (rcstream == NULL) {
1331
1332
	    /* Don't complain about the file's not existing. */
	    if (errno != ENOENT)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1333
1334
		rcfile_error(N_("Error reading %s: %s"), nanorc,
			strerror(errno));
1335
	} else
1336
	    parse_rcfile(rcstream
1337
#ifndef DISABLE_COLOR
1338
1339
1340
		, FALSE
#endif
		);
1341
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1342

1343
1344
    free(nanorc);
    nanorc = NULL;
1345

1346
1347
1348
1349
1350
1351
1352
1353
    if (errors && !ISSET(QUIET)) {
	errors = FALSE;
	fprintf(stderr,
		_("\nPress Enter to continue starting nano.\n"));
	while (getchar() != '\n')
	    ;
    }

1354
#ifndef DISABLE_COLOR
1355
1356
    set_colorpairs();
#endif
1357
1358
}

1359
#endif /* ENABLE_NANORC */