rcfile.c 17.7 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
41
#ifndef DISABLE_JUSTIFY
    {"brackets", 0},
42
#endif
43
    {"const", CONST_UPDATE},
Chris Allegretta's avatar
Chris Allegretta committed
44
#ifndef DISABLE_WRAPJUSTIFY
45
    {"fill", 0},
Chris Allegretta's avatar
Chris Allegretta committed
46
#endif
47
#ifndef DISABLE_MOUSE
48
    {"mouse", USE_MOUSE},
49
50
51
52
#endif
#ifdef ENABLE_MULTIBUFFER
    {"multibuffer", MULTIBUFFER},
#endif
53
    {"morespace", MORE_SPACE},
54
    {"nofollow", NOFOLLOW_SYMLINKS},
55
    {"nohelp", NO_HELP},
Chris Allegretta's avatar
Chris Allegretta committed
56
#ifndef DISABLE_WRAPPING
57
    {"nowrap", NO_WRAP},
Chris Allegretta's avatar
Chris Allegretta committed
58
#endif
59
#ifndef DISABLE_OPERATINGDIR
60
    {"operatingdir", 0},
61
#endif
Chris Allegretta's avatar
Chris Allegretta committed
62
    {"preserve", PRESERVE},
63
#ifndef DISABLE_JUSTIFY
64
    {"punct", 0},
65
66
    {"quotestr", 0},
#endif
67
    {"rebinddelete", REBIND_DELETE},
68
69
70
#ifdef HAVE_REGEX_H
    {"regexp", USE_REGEXP},
#endif
71
#ifndef DISABLE_SPELLER
72
    {"speller", 0},
73
74
75
#endif
    {"suspend", SUSPEND},
    {"tabsize", 0},
76
    {"tempfile", TEMP_FILE},
77
    {"view", VIEW_MODE},
78
#ifndef NANO_SMALL
79
80
81
82
83
84
85
86
    {"autoindent", AUTOINDENT},
    {"backup", BACKUP_FILE},
    {"backupdir", 0},
    {"backwards", BACKWARDS_SEARCH},
    {"casesensitive", CASE_SENSITIVE},
    {"cut", CUT_TO_END},
    {"historylog", HISTORYLOG},
    {"noconvert", NO_CONVERT},
87
    {"quickblank", QUICK_BLANK},
88
89
90
    {"smarthome", SMART_HOME},
    {"smooth", SMOOTH_SCROLL},
    {"tabstospaces", TABS_TO_SPACES},
91
92
    {"whitespace", 0},
#endif
93
    {NULL, 0}
94
};
95

96
static bool errors = FALSE;
97
static int lineno = 0;
98
99
100
101
102
103
104
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
105

106
/* We have an error in some part of the rcfile.  Put it on stderr and
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
107
 * make the user hit Return to continue starting up nano. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
108
void rcfile_error(const char *msg, ...)
109
110
111
112
{
    va_list ap;

    fprintf(stderr, "\n");
113
114
    if (lineno > 0) {
	errors = TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
115
	fprintf(stderr, _("Error in %s on line %d: "), nanorc, lineno);
116
    }
Chris Allegretta's avatar
Chris Allegretta committed
117

118
    va_start(ap, msg);
119
    vfprintf(stderr, _(msg), ap);
120
    va_end(ap);
121
122

    fprintf(stderr, "\n");
123
124
}

125
126
/* 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
127
char *parse_next_word(char *ptr)
128
{
129
    while (!isblank(*ptr) && *ptr != '\0')
130
131
132
	ptr++;

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

135
136
    /* Null-terminate and advance ptr. */
    *ptr++ = '\0';
137

138
    while (isblank(*ptr))
139
140
141
142
	ptr++;

    return ptr;
}
143

144
145
146
147
148
149
150
/* 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
151
152
char *parse_argument(char *ptr)
{
Chris Allegretta's avatar
Chris Allegretta committed
153
154
155
156
157
158
159
160
161
162
163
164
    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;
165
    } while (*ptr != '\0');
Chris Allegretta's avatar
Chris Allegretta committed
166
167
168
169
170
171

    if (last_quote == NULL) {
	if (*ptr == '\0')
	    ptr = NULL;
	else
	    *ptr++ = '\0';
172
	rcfile_error(N_("Argument %s has unterminated \""), ptr_bak);
Chris Allegretta's avatar
Chris Allegretta committed
173
174
175
176
177
    } else {
	*last_quote = '\0';
	ptr = last_quote + 1;
    }
    if (ptr != NULL)
178
	while (isblank(*ptr))
Chris Allegretta's avatar
Chris Allegretta committed
179
180
181
182
183
	    ptr++;
    return ptr;
}

#ifdef ENABLE_COLOR
184
int color_to_int(const char *colorname, bool *bright)
185
{
186
    int mcolor = -1;
187

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

190
    if (strncasecmp(colorname, "bright", 6) == 0) {
191
	*bright = TRUE;
192
193
	colorname += 6;
    }
194

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

218
219
220
    return mcolor;
}

221
222
char *parse_next_regex(char *ptr)
{
223
224
225
226
    assert(ptr != NULL);

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

231
232
233
    assert(*ptr == '"' || *ptr == '\0');

    if (*ptr == '\0') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
234
235
	rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
236
	return NULL;
237
    }
238

239
    /* Null terminate and advance ptr. */
240
241
    *ptr++ = '\0';

242
    while (isblank(*ptr))
243
244
245
246
247
	ptr++;

    return ptr;
}

248
/* Compile the regular expression regex to preg.  Return FALSE on
249
 * success, or TRUE if the expression is invalid. */
250
bool nregcomp(regex_t *preg, const char *regex, int eflags)
251
{
252
    int rc = regcomp(preg, regex, REG_EXTENDED | eflags);
253
254
255
256
257
258

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

	regerror(rc, preg, str, len);
259
	rcfile_error(N_("Bad regex \"%s\": %s"), regex, str);
260
261
	free(str);
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
262
263

    return (rc != 0);
264
265
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
266
void parse_syntax(char *ptr)
267
{
Chris Allegretta's avatar
Chris Allegretta committed
268
    const char *fileregptr = NULL, *nameptr = NULL;
269
270
    exttype *endext = NULL;
	/* The end of the extensions list for this syntax. */
271

272
    assert(ptr != NULL);
273

274
275
    if (*ptr == '\0') {
	rcfile_error(N_("Missing syntax name"));
276
	return;
277
    }
278
279

    if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
280
281
	rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
282
	return;
283
    }
284

285
286
287
288
289
    ptr++;

    nameptr = ptr;
    ptr = parse_next_regex(ptr);

290
    if (ptr == NULL)
291
	return;
292

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

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

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

320
	if (*ptr == '\0')
321
	    return;
322

323
324
325
326
	ptr++;

	fileregptr = ptr;
	ptr = parse_next_regex(ptr);
327
328
	if (ptr == NULL)
	    break;
329

330
	newext = (exttype *)nmalloc(sizeof(exttype));
331
	if (nregcomp(&newext->val, fileregptr, REG_NOSUB))
332
333
334
	    free(newext);
	else {
	    if (endext == NULL)
335
		endsyntax->extensions = newext;
336
337
338
339
	    else
		endext->next = newext;
	    endext = newext;
	    endext->next = NULL;
340
	}
341
    }
342
343
}

344
345
346
/* Parse the color stuff into the colorstrings array.  If icase is TRUE,
 * treat the color stuff as case insensitive. */
void parse_colors(char *ptr, bool icase)
347
{
348
    int fg, bg;
349
    bool bright = FALSE, no_fgcolor = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
350
    char *fgstr;
351

352
    assert(ptr != NULL);
353

354
    if (*ptr == '\0') {
355
	rcfile_error(N_("Missing color name"));
356
	return;
357
358
    }

359
360
361
362
    fgstr = ptr;
    ptr = parse_next_word(ptr);

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

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

383
384
    if (!no_fgcolor) {
	fg = color_to_int(fgstr, &bright);
385

386
387
388
389
390
	/* Don't try to parse screwed-up foreground colors. */
	if (fg == -1)
	    return;
    } else
	fg = -1;
391

392
    if (syntaxes == NULL) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
393
394
	rcfile_error(
		N_("Cannot add a color directive without a syntax line"));
395
	return;
396
397
    }

398
    if (*ptr == '\0') {
399
	rcfile_error(N_("Missing regex string"));
400
401
402
	return;
    }

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

413
	if (strncasecmp(ptr, "start=", 6) == 0) {
414
	    ptr += 6;
415
	    expectend = TRUE;
416
417
418
	}

	if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
419
420
	    rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
421
	    ptr = parse_next_regex(ptr);
422
423
	    continue;
	}
424

425
	ptr++;
426

427
428
429
	newcolor = (colortype *)nmalloc(sizeof(colortype));
	fgstr = ptr;
	ptr = parse_next_regex(ptr);
430
431
432
	if (ptr == NULL)
	    break;

433
	if (nregcomp(&newcolor->start, fgstr, icase ? REG_ICASE : 0)) {
434
	    free(newcolor);
435
	    cancelled = TRUE;
436
	} else {
437
438
439
440
441
442
	    newcolor->fg = fg;
	    newcolor->bg = bg;
	    newcolor->bright = bright;
	    newcolor->next = NULL;
	    newcolor->end = NULL;

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

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

470
471
	    ptr++;

472
	    fgstr = ptr;
473
	    ptr = parse_next_regex(ptr);
474
475
	    if (ptr == NULL)
		break;
476
477
478
479
480

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

482
	    newcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
483
	    if (nregcomp(newcolor->end, fgstr, icase ? REG_ICASE : 0)) {
484
485
486
		free(newcolor->end);
		newcolor->end = NULL;
	    }
Chris Allegretta's avatar
Chris Allegretta committed
487
	}
488
    }
489
}
490
#endif /* ENABLE_COLOR */
491

492
/* Parse the rcfile, once it has been opened successfully. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
493
void parse_rcfile(FILE *rcstream)
494
{
495
496
497
498
499
500
501
502
503
504
    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';
505
506
507

	lineno++;
	ptr = buf;
508
	while (isblank(*ptr))
509
510
	    ptr++;

511
512
513
	/* If we have a blank line or a comment, skip to the next
	 * line. */
	if (*ptr == '\0' || *ptr == '#')
514
515
	    continue;

516
	/* Otherwise, skip to the next space. */
517
518
519
	keyword = ptr;
	ptr = parse_next_word(ptr);

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

	if (set == 0)
	    continue;

	if (*ptr == '\0') {
	    rcfile_error(N_("Missing flag"));
541
542
543
544
545
546
	    continue;
	}

	option = ptr;
	ptr = parse_next_word(ptr);

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

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

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

Chris Allegretta's avatar
Chris Allegretta committed
682
    free(buf);
683
684
    fclose(rcstream);
    lineno = 0;
685
686
687

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

694
695
696
697
698
699
700
701
    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
702
703
#ifdef SYSCONFDIR
    assert(sizeof(SYSCONFDIR) == strlen(SYSCONFDIR) + 1);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
704

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

712
713
714
715
716
717
718
#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
719

720
    get_homedir();
721

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

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

738
739
    free(nanorc);
    nanorc = NULL;
740

741
742
743
#ifdef ENABLE_COLOR
    set_colorpairs();
#endif
744
745
}

746
#endif /* ENABLE_NANORC */