rcfile.c 18.8 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 <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
36
#include "proto.h"

#ifdef ENABLE_NANORC

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

96
static bool errors = FALSE;
97
static size_t 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;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
115
	fprintf(stderr, _("Error in %s on line %lu: "), nanorc, (unsigned long)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
short color_to_short(const char *colorname, bool *bright)
185
{
186
    short 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
249
250
/* Compile the regular expression regex to see if it's valid.  Return
 * TRUE if it is, or FALSE otherwise. */
bool nregcomp(const char *regex, int eflags)
251
{
252
253
    regex_t preg;
    int rc = regcomp(&preg, regex, REG_EXTENDED | eflags);
254
255

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

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

264
265
    regfree(&preg);

266
    return (rc == 0);
267
268
}

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

276
    assert(ptr != NULL);
277

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

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

289
290
291
292
293
    ptr++;

    nameptr = ptr;
    ptr = parse_next_regex(ptr);

294
    if (ptr == NULL)
295
	return;
296

297
298
    for (tmpsyntax = syntaxes; tmpsyntax != NULL;
	tmpsyntax = tmpsyntax->next) {
299
	if (strcmp(nameptr, tmpsyntax->desc) == 0) {
300
301
302
303
304
	    rcfile_error(N_("Duplicate syntax name %s"), nameptr);
	    return;
	}
    }

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

316
317
318
319
320
    endsyntax->desc = mallocstrcpy(NULL, nameptr);
    endsyntax->color = NULL;
    endcolor = NULL;
    endsyntax->extensions = NULL;
    endsyntax->next = NULL;
321

322
#ifdef DEBUG
323
    fprintf(stderr, "Starting a new syntax type: \"%s\"\n", nameptr);
324
325
#endif

326
327
328
329
330
331
332
    /* 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;
    }

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

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

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

348
	if (*ptr == '\0')
349
	    return;
350

351
352
353
354
	ptr++;

	fileregptr = ptr;
	ptr = parse_next_regex(ptr);
355
356
	if (ptr == NULL)
	    break;
357

358
	newext = (exttype *)nmalloc(sizeof(exttype));
359
360
361
362
363
364

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

365
	    if (endext == NULL)
366
		endsyntax->extensions = newext;
367
368
369
370
	    else
		endext->next = newext;
	    endext = newext;
	    endext->next = NULL;
371
372
	} else
	    free(newext);
373
    }
374
375
}

376
377
378
/* 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)
379
{
380
    short fg, bg;
381
    bool bright = FALSE, no_fgcolor = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
382
    char *fgstr;
383

384
    assert(ptr != NULL);
385

386
    if (*ptr == '\0') {
387
	rcfile_error(N_("Missing color name"));
388
	return;
389
390
    }

391
392
393
394
    fgstr = ptr;
    ptr = parse_next_word(ptr);

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

397
	strtok(fgstr, ",");
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
398
	bgcolorname = strtok(NULL, ",");
399
400
401
402
403
404
	if (bgcolorname == NULL) {
	    /* If we have a background color without a foreground color,
	     * parse it properly. */
	    bgcolorname = fgstr + 1;
	    no_fgcolor = TRUE;
	}
405
	if (strncasecmp(bgcolorname, "bright", 6) == 0) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
406
407
408
	    rcfile_error(
		N_("Background color %s cannot be bright"),
		bgcolorname);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
409
410
	    return;
	}
411
	bg = color_to_short(bgcolorname, &bright);
412
    } else
Chris Allegretta's avatar
Chris Allegretta committed
413
	bg = -1;
414

415
    if (!no_fgcolor) {
416
	fg = color_to_short(fgstr, &bright);
417

418
419
420
421
422
	/* Don't try to parse screwed-up foreground colors. */
	if (fg == -1)
	    return;
    } else
	fg = -1;
423

424
    if (syntaxes == NULL) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
425
426
	rcfile_error(
		N_("Cannot add a color directive without a syntax line"));
427
	return;
428
429
    }

430
    if (*ptr == '\0') {
431
	rcfile_error(N_("Missing regex string"));
432
433
434
	return;
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
435
    /* Now for the fun part.  Start adding regexes to individual strings
436
437
     * in the colorstrings array, woo! */
    while (ptr != NULL && *ptr != '\0') {
438
439
	colortype *newcolor;
	    /* The new color structure. */
440
	bool cancelled = FALSE;
441
	    /* The start expression was bad. */
442
443
	bool expectend = FALSE;
	    /* Do we expect an end= line? */
444

445
	if (strncasecmp(ptr, "start=", 6) == 0) {
446
	    ptr += 6;
447
	    expectend = TRUE;
448
449
450
	}

	if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
451
452
	    rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
453
	    ptr = parse_next_regex(ptr);
454
455
	    continue;
	}
456

457
	ptr++;
458

459
460
	fgstr = ptr;
	ptr = parse_next_regex(ptr);
461
462
463
	if (ptr == NULL)
	    break;

464
	newcolor = (colortype *)nmalloc(sizeof(colortype));
465

466
467
468
	/* Save the starting regex string if it's valid, and set up the
	 * color information. */
	if (nregcomp(fgstr, icase ? REG_ICASE : 0)) {
469
470
471
	    newcolor->fg = fg;
	    newcolor->bg = bg;
	    newcolor->bright = bright;
472
	    newcolor->icase = icase;
473
474
475
476

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

477
	    newcolor->end_regex = NULL;
478
	    newcolor->end = NULL;
479

480
	    newcolor->next = NULL;
481

482
483
	    if (endcolor == NULL) {
		endsyntax->color = newcolor;
484
#ifdef DEBUG
485
		fprintf(stderr, "Starting a new colorstring for fg %hd, bg %hd\n", fg, bg);
486
#endif
487
	    } else {
Chris Allegretta's avatar
Chris Allegretta committed
488
#ifdef DEBUG
489
		fprintf(stderr, "Adding new entry for fg %hd, bg %hd\n", fg, bg);
Chris Allegretta's avatar
Chris Allegretta committed
490
#endif
491
		endcolor->next = newcolor;
492
	    }
493

494
	    endcolor = newcolor;
495
496
497
	} else {
	    free(newcolor);
	    cancelled = TRUE;
498
	}
Chris Allegretta's avatar
Chris Allegretta committed
499

500
	if (expectend) {
501
	    if (ptr == NULL || strncasecmp(ptr, "end=", 4) != 0) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
502
503
		rcfile_error(
			N_("\"start=\" requires a corresponding \"end=\""));
504
505
506
507
		return;
	    }
	    ptr += 4;
	    if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
508
509
		rcfile_error(
			N_("Regex strings must begin and end with a \" character"));
510
		continue;
511
	    }
512

513
514
	    ptr++;

515
	    fgstr = ptr;
516
	    ptr = parse_next_regex(ptr);
517
518
	    if (ptr == NULL)
		break;
519
520
521
522
523

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

525
526
527
	    /* Save the ending regex string if it's valid. */
	    newcolor->end_regex = (nregcomp(fgstr, icase ? REG_ICASE :
		0)) ? mallocstrcpy(NULL, fgstr) : NULL;
Chris Allegretta's avatar
Chris Allegretta committed
528
	}
529
    }
530
}
531
#endif /* ENABLE_COLOR */
532

533
/* Parse the rcfile, once it has been opened successfully. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
534
void parse_rcfile(FILE *rcstream)
535
{
536
537
538
539
540
541
542
543
544
545
    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';
546
547
548

	lineno++;
	ptr = buf;
549
	while (isblank(*ptr))
550
551
	    ptr++;

552
553
554
	/* If we have a blank line or a comment, skip to the next
	 * line. */
	if (*ptr == '\0' || *ptr == '#')
555
556
	    continue;

557
	/* Otherwise, skip to the next space. */
558
559
560
	keyword = ptr;
	ptr = parse_next_word(ptr);

561
	/* Try to parse the keyword. */
562
	if (strcasecmp(keyword, "set") == 0)
563
	    set = 1;
564
	else if (strcasecmp(keyword, "unset") == 0)
565
	    set = -1;
566
#ifdef ENABLE_COLOR
567
	else if (strcasecmp(keyword, "syntax") == 0)
Chris Allegretta's avatar
Chris Allegretta committed
568
	    parse_syntax(ptr);
569
	else if (strcasecmp(keyword, "color") == 0)
570
571
572
	    parse_colors(ptr, FALSE);
	else if (strcasecmp(keyword, "icolor") == 0)
	    parse_colors(ptr, TRUE);
573
574
#endif /* ENABLE_COLOR */
	else
575
	    rcfile_error(N_("Command %s not understood"), keyword);
576
577
578
579
580
581

	if (set == 0)
	    continue;

	if (*ptr == '\0') {
	    rcfile_error(N_("Missing flag"));
582
583
584
585
586
587
	    continue;
	}

	option = ptr;
	ptr = parse_next_word(ptr);

588
589
	for (i = 0; rcopts[i].name != NULL; i++) {
	    if (strcasecmp(option, rcopts[i].name) == 0) {
590
#ifdef DEBUG
591
592
593
594
595
596
597
598
599
600
601
		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
602
603
604
			    rcfile_error(
				N_("Option %s requires an argument"),
				rcopts[i].name);
605
606
607
608
609
610
			    break;
			}
			option = ptr;
			if (*option == '"')
			    option++;
			ptr = parse_argument(ptr);
611

612
			option = mallocstrcpy(NULL, option);
Chris Allegretta's avatar
Chris Allegretta committed
613
#ifdef DEBUG
614
			fprintf(stderr, "option = \"%s\"\n", option);
Chris Allegretta's avatar
Chris Allegretta committed
615
#endif
616
617
618
619
620
621
622
623
624

			/* 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
625
#ifndef DISABLE_OPERATINGDIR
626
			if (strcasecmp(rcopts[i].name, "operatingdir") == 0)
627
			    operating_dir = option;
628
			else
Chris Allegretta's avatar
Chris Allegretta committed
629
#endif
630
#ifndef DISABLE_WRAPJUSTIFY
631
632
			if (strcasecmp(rcopts[i].name, "fill") == 0) {
			    if (!parse_num(option, &wrap_at)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
633
634
635
				rcfile_error(
					N_("Requested fill size %s invalid"),
					option);
636
				wrap_at = -CHARS_FROM_EOL;
637
638
			    } else
				free(option);
639
			} else
Chris Allegretta's avatar
Chris Allegretta committed
640
#endif
641
#ifndef NANO_SMALL
642
			if (strcasecmp(rcopts[i].name, "whitespace") == 0) {
643
			    whitespace = option;
644
645
			    if (mbstrlen(whitespace) != 2 ||
				strlenpt(whitespace) != 2) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
646
647
				rcfile_error(
					N_("Two single-column characters required"));
648
649
				free(whitespace);
				whitespace = NULL;
650
651
652
			    } else {
				whitespace_len[0] =
					parse_mbchar(whitespace, NULL,
653
					NULL);
654
655
				whitespace_len[1] =
					parse_mbchar(whitespace +
656
					whitespace_len[0], NULL, NULL);
657
658
			    }
			} else
659
#endif
660
#ifndef DISABLE_JUSTIFY
661
			if (strcasecmp(rcopts[i].name, "punct") == 0) {
662
			    punct = option;
663
			    if (has_blank_mbchars(punct)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
664
				rcfile_error(
665
					N_("Non-blank characters required"));
666
667
668
				free(punct);
				punct = NULL;
			    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
669
670
			} else if (strcasecmp(rcopts[i].name,
				"brackets") == 0) {
671
			    brackets = option;
672
			    if (has_blank_mbchars(brackets)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
673
				rcfile_error(
674
					N_("Non-blank characters required"));
675
676
677
				free(brackets);
				brackets = NULL;
			    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
678
679
			} else if (strcasecmp(rcopts[i].name,
				"quotestr") == 0)
680
			    quotestr = option;
681
			else
682
#endif
683
#ifndef NANO_SMALL
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
684
685
			if (strcasecmp(rcopts[i].name,
				"backupdir") == 0)
686
			    backup_dir = option;
687
			else
688
#endif
689
#ifndef DISABLE_SPELLER
690
			if (strcasecmp(rcopts[i].name, "speller") == 0)
691
			    alt_speller = option;
692
693
			else
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
694
695
696
697
698
699
700
			if (strcasecmp(rcopts[i].name,
				"tabsize") == 0) {
			    if (!parse_num(option, &tabsize) ||
				tabsize <= 0) {
				rcfile_error(
					N_("Requested tab size %s invalid"),
					option);
701
				tabsize = -1;
702
703
			    } else
				free(option);
704
			} else
705
706
			    assert(FALSE);
		    }
707
#ifdef DEBUG
708
		    fprintf(stderr, "flag = %ld\n", rcopts[i].flag);
709
#endif
710
711
712
		} else if (rcopts[i].flag != 0)
		    UNSET(rcopts[i].flag);
		else
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
713
714
		    rcfile_error(N_("Cannot unset flag %s"),
			rcopts[i].name);
715
		break;
716
717
	    }
	}
718
719
	if (rcopts[i].name == NULL)
	    rcfile_error(N_("Unknown flag %s"), option);
720
    }
721

Chris Allegretta's avatar
Chris Allegretta committed
722
    free(buf);
723
724
    fclose(rcstream);
    lineno = 0;
725
726
727

    if (errors) {
	errors = FALSE;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
728
	fprintf(stderr, _("\nPress Return to continue starting nano\n"));
729
730
731
732
	while (getchar() != '\n')
	    ;
    }

733
734
735
736
737
738
739
740
    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
741
742
#ifdef SYSCONFDIR
    assert(sizeof(SYSCONFDIR) == strlen(SYSCONFDIR) + 1);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
743

744
745
    nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc");
    /* Try to open the system-wide nanorc. */
746
    rcstream = fopen(nanorc, "r");
747
    if (rcstream != NULL)
Chris Allegretta's avatar
Chris Allegretta committed
748
749
	parse_rcfile(rcstream);
#endif
750

751
752
753
754
755
756
757
#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
758

759
    get_homedir();
760

761
    if (homedir == NULL)
762
	rcfile_error(N_("I can't find my home directory!  Wah!"));
763
764
765
    else {
	nanorc = charealloc(nanorc, strlen(homedir) + 9);
	sprintf(nanorc, "%s/.nanorc", homedir);
766
	rcstream = fopen(nanorc, "r");
767

768
	if (rcstream == NULL) {
769
770
	    /* Don't complain about the file's not existing. */
	    if (errno != ENOENT)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
771
772
		rcfile_error(N_("Error reading %s: %s"), nanorc,
			strerror(errno));
773
	} else
Chris Allegretta's avatar
Chris Allegretta committed
774
	    parse_rcfile(rcstream);
775
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
776

777
778
    free(nanorc);
    nanorc = NULL;
779

780
781
782
#ifdef ENABLE_COLOR
    set_colorpairs();
#endif
783
784
}

785
#endif /* ENABLE_NANORC */