rcfile.c 17.5 KB
Newer Older
1
2
/* $Id$ */
/**************************************************************************
3
 *   rcfile.c                                                             *
4
 *                                                                        *
5
 *   Copyright (C) 1999-2005 Chris Allegretta                             *
6
7
 *   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 *
8
 *   the Free Software Foundation; either version 2, or (at your option)  *
9
10
 *   any later version.                                                   *
 *                                                                        *
11
12
13
14
 *   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.                             *
15
16
17
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
18
19
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA            *
 *   02110-1301, USA.                                                     *
20
21
22
 *                                                                        *
 **************************************************************************/

23
24
25
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
26

27
#include <stdlib.h>
28
#include <stdarg.h>
29
30
31
#include <string.h>
#include <stdio.h>
#include <errno.h>
Chris Allegretta's avatar
Chris Allegretta committed
32
#include <unistd.h>
33
#include <ctype.h>
Chris Allegretta's avatar
Chris Allegretta committed
34
#include <assert.h>
35
36
37
38
#include "proto.h"

#ifdef ENABLE_NANORC

39
const static rcoption rcopts[] = {
40
#ifndef NANO_SMALL
41
    {"autoindent", AUTOINDENT},
42
    {"backup", BACKUP_FILE},
43
    {"backupdir", 0},
44
45
46
#endif
#ifndef DISABLE_JUSTIFY
    {"brackets", 0},
47
48
49
#endif
    {"const", CONSTUPDATE},
#ifndef NANO_SMALL
50
    {"cut", CUT_TO_END},
51
#endif
Chris Allegretta's avatar
Chris Allegretta committed
52
#ifndef DISABLE_WRAPJUSTIFY
53
    {"fill", 0},
Chris Allegretta's avatar
Chris Allegretta committed
54
#endif
55
56
57
#ifndef NANO_SMALL
    {"historylog", HISTORYLOG},
#endif
58
#ifndef DISABLE_MOUSE
59
    {"mouse", USE_MOUSE},
60
61
62
63
#endif
#ifdef ENABLE_MULTIBUFFER
    {"multibuffer", MULTIBUFFER},
#endif
64
    {"morespace", MORE_SPACE},
65
66
67
#ifndef NANO_SMALL
    {"noconvert", NO_CONVERT},
#endif
68
    {"nofollow", NOFOLLOW_SYMLINKS},
69
    {"nohelp", NO_HELP},
Chris Allegretta's avatar
Chris Allegretta committed
70
#ifndef DISABLE_WRAPPING
71
    {"nowrap", NO_WRAP},
Chris Allegretta's avatar
Chris Allegretta committed
72
#endif
73
#ifndef DISABLE_OPERATINGDIR
74
    {"operatingdir", 0},
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
#ifndef NANO_SMALL
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
83
    {"smarthome", SMART_HOME},
84
    {"smooth", SMOOTH_SCROLL},
85
86
#endif
#ifndef DISABLE_SPELLER
87
    {"speller", 0},
88
89
90
#endif
    {"suspend", SUSPEND},
    {"tabsize", 0},
91
92
93
#ifndef NANO_SMALL
    {"tabstospaces", TABS_TO_SPACES},
#endif
94
    {"tempfile", TEMP_FILE},
95
    {"view", VIEW_MODE},
96
97
98
#ifndef NANO_SMALL
    {"whitespace", 0},
#endif
99
    {NULL, 0}
100
};
101

102
static bool errors = FALSE;
103
static int lineno = 0;
104
105
106
107
108
109
110
static char *nanorc = NULL;
#ifdef ENABLE_COLOR
static syntaxtype *endsyntax = NULL;
	/* The end of the list of syntaxes. */
static colortype *endcolor = NULL;
	/* The end of the color list for the current syntax. */
#endif
111

112
/* We have an error in some part of the rcfile.  Put it on stderr and
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
113
 * make the user hit Return to continue starting up nano. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
114
void rcfile_error(const char *msg, ...)
115
116
117
118
{
    va_list ap;

    fprintf(stderr, "\n");
119
120
    if (lineno > 0) {
	errors = TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
121
	fprintf(stderr, _("Error in %s on line %d: "), nanorc, lineno);
122
    }
Chris Allegretta's avatar
Chris Allegretta committed
123

124
    va_start(ap, msg);
125
    vfprintf(stderr, _(msg), ap);
126
    va_end(ap);
127
128

    fprintf(stderr, "\n");
129
130
}

131
132
/* Parse the next word from the string.  Return points to '\0' if we hit
 * the end of the line. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
133
char *parse_next_word(char *ptr)
134
{
135
    while (!isblank(*ptr) && *ptr != '\0')
136
137
138
	ptr++;

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

141
142
    /* Null-terminate and advance ptr. */
    *ptr++ = '\0';
143

144
    while (isblank(*ptr))
145
146
147
148
	ptr++;

    return ptr;
}
149

150
151
152
153
154
155
156
/* The keywords operatingdir, backupdir, fill, tabsize, speller, punct,
 * brackets, quotestr, and whitespace take an argument when set.  Among
 * these, operatingdir, backupdir, speller, punct, brackets, quotestr,
 * and whitespace have to allow tabs and spaces in the argument.  Thus,
 * if the next word starts with a ", we say it ends with the last " of
 * the line.  Otherwise, the word is interpreted as usual.  That is so
 * the arguments can contain "s too. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
157
158
char *parse_argument(char *ptr)
{
Chris Allegretta's avatar
Chris Allegretta committed
159
160
161
162
163
164
165
166
167
168
169
170
    const char *ptr_bak = ptr;
    char *last_quote = NULL;

    assert(ptr != NULL);

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

    do {
	ptr++;
	if (*ptr == '"')
	    last_quote = ptr;
171
    } while (*ptr != '\0');
Chris Allegretta's avatar
Chris Allegretta committed
172
173
174
175
176
177

    if (last_quote == NULL) {
	if (*ptr == '\0')
	    ptr = NULL;
	else
	    *ptr++ = '\0';
178
	rcfile_error(N_("Argument %s has unterminated \""), ptr_bak);
Chris Allegretta's avatar
Chris Allegretta committed
179
180
181
182
183
    } else {
	*last_quote = '\0';
	ptr = last_quote + 1;
    }
    if (ptr != NULL)
184
	while (isblank(*ptr))
Chris Allegretta's avatar
Chris Allegretta committed
185
186
187
188
189
	    ptr++;
    return ptr;
}

#ifdef ENABLE_COLOR
190
int color_to_int(const char *colorname, bool *bright)
191
{
192
    int mcolor = -1;
193

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

196
    if (strncasecmp(colorname, "bright", 6) == 0) {
197
	*bright = TRUE;
198
199
	colorname += 6;
    }
200

201
    if (strcasecmp(colorname, "green") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
202
	mcolor = COLOR_GREEN;
203
    else if (strcasecmp(colorname, "red") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
204
	mcolor = COLOR_RED;
205
    else if (strcasecmp(colorname, "blue") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
206
	mcolor = COLOR_BLUE;
207
    else if (strcasecmp(colorname, "white") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
208
	mcolor = COLOR_WHITE;
209
    else if (strcasecmp(colorname, "yellow") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
210
	mcolor = COLOR_YELLOW;
211
    else if (strcasecmp(colorname, "cyan") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
212
	mcolor = COLOR_CYAN;
213
    else if (strcasecmp(colorname, "magenta") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
214
	mcolor = COLOR_MAGENTA;
215
    else if (strcasecmp(colorname, "black") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
216
	mcolor = COLOR_BLACK;
217
    else
218
	rcfile_error(N_("Color %s not understood.\n"
219
220
221
		"Valid colors are \"green\", \"red\", \"blue\",\n"
		"\"white\", \"yellow\", \"cyan\", \"magenta\" and\n"
		"\"black\", with the optional prefix \"bright\"\n"
222
		"for foreground colors."), colorname);
223

224
225
226
    return mcolor;
}

227
228
char *parse_next_regex(char *ptr)
{
229
230
231
232
    assert(ptr != NULL);

    /* Continue until the end of the line, or a " followed by a space, a
     * blank character, or \0. */
233
    while ((*ptr != '"' || (!isblank(*(ptr + 1)) &&
234
	*(ptr + 1) != '\0')) && *ptr != '\0')
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

245
    /* Null terminate and advance ptr. */
246
247
    *ptr++ = '\0';

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

    return ptr;
}

254
/* Compile the regular expression regex to preg.  Return FALSE on
255
 * success, or TRUE if the expression is invalid. */
256
bool nregcomp(regex_t *preg, const char *regex, int eflags)
257
{
258
    int rc = regcomp(preg, regex, REG_EXTENDED | eflags);
259
260
261
262
263
264

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

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

    return (rc != 0);
270
271
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
272
void parse_syntax(char *ptr)
273
{
Chris Allegretta's avatar
Chris Allegretta committed
274
    const char *fileregptr = NULL, *nameptr = NULL;
275
276
    exttype *endext = NULL;
	/* The end of the extensions list for this syntax. */
277

278
    assert(ptr != NULL);
279

280
281
    if (*ptr == '\0') {
	rcfile_error(N_("Missing syntax name"));
282
	return;
283
    }
284
285

    if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
286
287
	rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
288
	return;
289
    }
290

291
292
293
294
295
    ptr++;

    nameptr = ptr;
    ptr = parse_next_regex(ptr);

296
    if (ptr == NULL)
297
	return;
298

Chris Allegretta's avatar
Chris Allegretta committed
299
300
    if (syntaxes == NULL) {
	syntaxes = (syntaxtype *)nmalloc(sizeof(syntaxtype));
301
	endsyntax = syntaxes;
Chris Allegretta's avatar
Chris Allegretta committed
302
    } else {
303
304
	endsyntax->next = (syntaxtype *)nmalloc(sizeof(syntaxtype));
	endsyntax = endsyntax->next;
305
#ifdef DEBUG
306
	fprintf(stderr, "Adding new syntax after first one\n");
307
#endif
Chris Allegretta's avatar
Chris Allegretta committed
308
    }
309
310
311
312
313
    endsyntax->desc = mallocstrcpy(NULL, nameptr);
    endsyntax->color = NULL;
    endcolor = NULL;
    endsyntax->extensions = NULL;
    endsyntax->next = NULL;
314
#ifdef DEBUG
315
    fprintf(stderr, "Starting a new syntax type: \"%s\"\n", nameptr);
316
317
#endif

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

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

326
	if (*ptr == '\0')
327
	    return;
328

329
330
331
332
	ptr++;

	fileregptr = ptr;
	ptr = parse_next_regex(ptr);
333
334
	if (ptr == NULL)
	    break;
335

336
	newext = (exttype *)nmalloc(sizeof(exttype));
337
	if (nregcomp(&newext->val, fileregptr, REG_NOSUB))
338
339
340
	    free(newext);
	else {
	    if (endext == NULL)
341
		endsyntax->extensions = newext;
342
343
344
345
	    else
		endext->next = newext;
	    endext = newext;
	    endext->next = NULL;
346
	}
347
    }
348
349
}

350
/* Parse the color stuff into the colorstrings array. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
351
void parse_colors(char *ptr)
352
{
353
    int fg, bg;
354
    bool bright = FALSE, no_fgcolor = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
355
    char *fgstr;
356

357
    assert(ptr != NULL);
358

359
    if (*ptr == '\0') {
360
	rcfile_error(N_("Missing color name"));
361
	return;
362
363
    }

364
365
366
367
    fgstr = ptr;
    ptr = parse_next_word(ptr);

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

370
	strtok(fgstr, ",");
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
371
	bgcolorname = strtok(NULL, ",");
372
373
374
375
376
377
	if (bgcolorname == NULL) {
	    /* If we have a background color without a foreground color,
	     * parse it properly. */
	    bgcolorname = fgstr + 1;
	    no_fgcolor = TRUE;
	}
378
	if (strncasecmp(bgcolorname, "bright", 6) == 0) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
379
380
381
	    rcfile_error(
		N_("Background color %s cannot be bright"),
		bgcolorname);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
382
383
	    return;
	}
384
	bg = color_to_int(bgcolorname, &bright);
385
    } else
Chris Allegretta's avatar
Chris Allegretta committed
386
	bg = -1;
387

388
389
    if (!no_fgcolor) {
	fg = color_to_int(fgstr, &bright);
390

391
392
393
394
395
	/* Don't try to parse screwed-up foreground colors. */
	if (fg == -1)
	    return;
    } else
	fg = -1;
396

397
    if (syntaxes == NULL) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
398
399
	rcfile_error(
		N_("Cannot add a color directive without a syntax line"));
400
	return;
401
402
    }

403
    if (*ptr == '\0') {
404
	rcfile_error(N_("Missing regex string"));
405
406
407
	return;
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
408
    /* Now for the fun part.  Start adding regexes to individual strings
409
410
     * in the colorstrings array, woo! */
    while (ptr != NULL && *ptr != '\0') {
411
412
	colortype *newcolor;
	    /* The new color structure. */
413
	bool cancelled = FALSE;
414
	    /* The start expression was bad. */
415
416
	bool expectend = FALSE;
	    /* Do we expect an end= line? */
417

418
	if (strncasecmp(ptr, "start=", 6) == 0) {
419
	    ptr += 6;
420
	    expectend = TRUE;
421
422
423
	}

	if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
424
425
	    rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
426
	    ptr = parse_next_regex(ptr);
427
428
	    continue;
	}
429

430
	ptr++;
431

432
433
434
	newcolor = (colortype *)nmalloc(sizeof(colortype));
	fgstr = ptr;
	ptr = parse_next_regex(ptr);
435
436
437
438
	if (ptr == NULL)
	    break;

	if (nregcomp(&newcolor->start, fgstr, 0)) {
439
	    free(newcolor);
440
	    cancelled = TRUE;
441
	} else {
442
443
444
445
446
447
	    newcolor->fg = fg;
	    newcolor->bg = bg;
	    newcolor->bright = bright;
	    newcolor->next = NULL;
	    newcolor->end = NULL;

448
449
	    if (endcolor == NULL) {
		endsyntax->color = newcolor;
450
#ifdef DEBUG
451
		fprintf(stderr, "Starting a new colorstring for fg %d, bg %d\n", fg, bg);
452
#endif
453
	    } else {
Chris Allegretta's avatar
Chris Allegretta committed
454
#ifdef DEBUG
455
		fprintf(stderr, "Adding new entry for fg %d, bg %d\n", fg, bg);
Chris Allegretta's avatar
Chris Allegretta committed
456
#endif
457
		endcolor->next = newcolor;
458
	    }
459
	    endcolor = newcolor;
460
	}
Chris Allegretta's avatar
Chris Allegretta committed
461

462
	if (expectend) {
463
	    if (ptr == NULL || strncasecmp(ptr, "end=", 4) != 0) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
464
465
		rcfile_error(
			N_("\"start=\" requires a corresponding \"end=\""));
466
467
468
469
		return;
	    }
	    ptr += 4;
	    if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
470
471
		rcfile_error(
			N_("Regex strings must begin and end with a \" character"));
472
		continue;
473
	    }
474

475
476
	    ptr++;

477
	    fgstr = ptr;
478
	    ptr = parse_next_regex(ptr);
479
480
	    if (ptr == NULL)
		break;
481
482
483
484
485

	    /* If the start regex was invalid, skip past the end regex to
	     * stay in sync. */
	    if (cancelled)
		continue;
486

487
	    newcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
488
	    if (nregcomp(newcolor->end, fgstr, 0)) {
489
490
491
		free(newcolor->end);
		newcolor->end = NULL;
	    }
Chris Allegretta's avatar
Chris Allegretta committed
492
	}
493
    }
494
}
495
#endif /* ENABLE_COLOR */
496

497
/* Parse the rcfile, once it has been opened successfully. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
498
void parse_rcfile(FILE *rcstream)
499
{
500
501
502
503
504
505
506
507
508
509
    char *buf = NULL;
    ssize_t len;
    size_t n;

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

	/* Ignore the \n. */
	buf[len - 1] = '\0';
510
511
512

	lineno++;
	ptr = buf;
513
	while (isblank(*ptr))
514
515
	    ptr++;

516
517
518
	/* If we have a blank line or a comment, skip to the next
	 * line. */
	if (*ptr == '\0' || *ptr == '#')
519
520
	    continue;

521
	/* Otherwise, skip to the next space. */
522
523
524
	keyword = ptr;
	ptr = parse_next_word(ptr);

525
	/* Try to parse the keyword. */
526
	if (strcasecmp(keyword, "set") == 0)
527
	    set = 1;
528
	else if (strcasecmp(keyword, "unset") == 0)
529
	    set = -1;
530
#ifdef ENABLE_COLOR
531
	else if (strcasecmp(keyword, "syntax") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
532
	    parse_syntax(ptr);
533
	else if (strcasecmp(keyword, "color") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
534
	    parse_colors(ptr);
535
536
#endif /* ENABLE_COLOR */
	else
537
	    rcfile_error(N_("Command %s not understood"), keyword);
538
539
540
541
542
543

	if (set == 0)
	    continue;

	if (*ptr == '\0') {
	    rcfile_error(N_("Missing flag"));
544
545
546
547
548
549
	    continue;
	}

	option = ptr;
	ptr = parse_next_word(ptr);

550
551
	for (i = 0; rcopts[i].name != NULL; i++) {
	    if (strcasecmp(option, rcopts[i].name) == 0) {
552
#ifdef DEBUG
553
554
555
556
557
558
559
560
561
562
563
		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
			 *an argument. */
			if (*ptr == '\0') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
564
565
566
			    rcfile_error(
				N_("Option %s requires an argument"),
				rcopts[i].name);
567
568
569
570
571
572
			    break;
			}
			option = ptr;
			if (*option == '"')
			    option++;
			ptr = parse_argument(ptr);
573

574
			option = mallocstrcpy(NULL, option);
Chris Allegretta's avatar
Chris Allegretta committed
575
#ifdef DEBUG
576
			fprintf(stderr, "option = \"%s\"\n", option);
Chris Allegretta's avatar
Chris Allegretta committed
577
#endif
578
579
580
581
582
583
584
585
586

			/* 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
587
#ifndef DISABLE_OPERATINGDIR
588
			if (strcasecmp(rcopts[i].name, "operatingdir") == 0)
589
			    operating_dir = option;
590
			else
Chris Allegretta's avatar
Chris Allegretta committed
591
#endif
592
#ifndef DISABLE_WRAPJUSTIFY
593
594
			if (strcasecmp(rcopts[i].name, "fill") == 0) {
			    if (!parse_num(option, &wrap_at)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
595
596
597
				rcfile_error(
					N_("Requested fill size %s invalid"),
					option);
598
				wrap_at = -CHARS_FROM_EOL;
599
600
			    } else
				free(option);
601
			} else
Chris Allegretta's avatar
Chris Allegretta committed
602
#endif
603
#ifndef NANO_SMALL
604
			if (strcasecmp(rcopts[i].name, "whitespace") == 0) {
605
			    whitespace = option;
606
607
			    if (mbstrlen(whitespace) != 2 ||
				strlenpt(whitespace) != 2) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
608
609
				rcfile_error(
					N_("Two single-column characters required"));
610
611
				free(whitespace);
				whitespace = NULL;
612
613
614
615
616
617
618
619
			    } else {
				whitespace_len[0] =
					parse_mbchar(whitespace, NULL,
					NULL, NULL);
				whitespace_len[1] =
					parse_mbchar(whitespace +
					whitespace_len[0], NULL,
					NULL, NULL);
620
621
			    }
			} else
622
#endif
623
#ifndef DISABLE_JUSTIFY
624
			if (strcasecmp(rcopts[i].name, "punct") == 0) {
625
			    punct = option;
626
			    if (has_blank_mbchars(punct)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
627
				rcfile_error(
628
					N_("Non-blank characters required"));
629
630
631
				free(punct);
				punct = NULL;
			    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
632
633
			} else if (strcasecmp(rcopts[i].name,
				"brackets") == 0) {
634
			    brackets = option;
635
			    if (has_blank_mbchars(brackets)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
636
				rcfile_error(
637
					N_("Non-blank characters required"));
638
639
640
				free(brackets);
				brackets = NULL;
			    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
641
642
			} else if (strcasecmp(rcopts[i].name,
				"quotestr") == 0)
643
			    quotestr = option;
644
			else
645
#endif
646
#ifndef NANO_SMALL
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
647
648
			if (strcasecmp(rcopts[i].name,
				"backupdir") == 0)
649
			    backup_dir = option;
650
			else
651
#endif
652
#ifndef DISABLE_SPELLER
653
			if (strcasecmp(rcopts[i].name, "speller") == 0)
654
			    alt_speller = option;
655
656
			else
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
657
658
659
660
661
662
663
			if (strcasecmp(rcopts[i].name,
				"tabsize") == 0) {
			    if (!parse_num(option, &tabsize) ||
				tabsize <= 0) {
				rcfile_error(
					N_("Requested tab size %s invalid"),
					option);
664
				tabsize = -1;
665
666
			    } else
				free(option);
667
			} else
668
669
			    assert(FALSE);
		    }
670
#ifdef DEBUG
671
		    fprintf(stderr, "flag = %ld\n", rcopts[i].flag);
672
#endif
673
674
675
		} else if (rcopts[i].flag != 0)
		    UNSET(rcopts[i].flag);
		else
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
676
677
		    rcfile_error(N_("Cannot unset flag %s"),
			rcopts[i].name);
678
		break;
679
680
	    }
	}
681
682
	if (rcopts[i].name == NULL)
	    rcfile_error(N_("Unknown flag %s"), option);
683
    }
684

Chris Allegretta's avatar
Chris Allegretta committed
685
    free(buf);
686
687
    fclose(rcstream);
    lineno = 0;
688
689
690

    if (errors) {
	errors = FALSE;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
691
692
	fprintf(stderr,
		_("\nPress Return to continue starting nano\n"));
693
694
695
696
	while (getchar() != '\n')
	    ;
    }

697
698
699
700
701
702
703
704
    return;
}

/* The main rc file function, tries to open the rc file */
void do_rcfile(void)
{
    FILE *rcstream;

Chris Allegretta's avatar
Chris Allegretta committed
705
706
#ifdef SYSCONFDIR
    assert(sizeof(SYSCONFDIR) == strlen(SYSCONFDIR) + 1);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
707

708
709
    nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc");
    /* Try to open the system-wide nanorc. */
710
    rcstream = fopen(nanorc, "r");
711
    if (rcstream != NULL)
Chris Allegretta's avatar
Chris Allegretta committed
712
713
	parse_rcfile(rcstream);
#endif
714

715
716
717
718
719
720
721
#if defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING)
    /* We've already read SYSCONFDIR/nanorc, if it's there.  If we're
     * root and --disable-wrapping-as-root is used, turn wrapping
     * off now. */
    if (geteuid() == NANO_ROOT_UID)
	SET(NO_WRAP);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
722

723
    get_homedir();
724

725
    if (homedir == NULL)
726
	rcfile_error(N_("I can't find my home directory!  Wah!"));
727
728
729
    else {
	nanorc = charealloc(nanorc, strlen(homedir) + 9);
	sprintf(nanorc, "%s/.nanorc", homedir);
730
	rcstream = fopen(nanorc, "r");
731

732
	if (rcstream == NULL) {
733
734
	    /* Don't complain about the file's not existing. */
	    if (errno != ENOENT)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
735
736
		rcfile_error(N_("Error reading %s: %s"), nanorc,
			strerror(errno));
737
	} else
Chris Allegretta's avatar
Chris Allegretta committed
738
	    parse_rcfile(rcstream);
739
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
740

741
742
    free(nanorc);
    nanorc = NULL;
743

744
745
746
#ifdef ENABLE_COLOR
    set_colorpairs();
#endif
747
748
}

749
#endif /* ENABLE_NANORC */