rcfile.c 34.5 KB
Newer Older
1
/**************************************************************************
2
 *   rcfile.c  --  This file is part of GNU nano.                         *
3
 *                                                                        *
4
5
 *   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,  *
 *   2010, 2011, 2013, 2014 Free Software Foundation, Inc.                *
6
7
8
 *   Copyright (C) 2014 Mike Frysinger                                    *
 *   Copyright (C) 2014, 2015, 2016 Benno Schulenberg                     *
 *                                                                        *
9
10
11
12
 *   GNU nano is free software: you can redistribute it and/or modify     *
 *   it under the terms of the GNU General Public License as published    *
 *   by the Free Software Foundation, either version 3 of the License,    *
 *   or (at your option) any later version.                               *
13
 *                                                                        *
14
15
16
17
 *   GNU nano is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty          *
 *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              *
 *   See the GNU General Public License for more details.                 *
18
19
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
20
 *   along with this program.  If not, see http://www.gnu.org/licenses/.  *
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
#ifndef DISABLE_NANORC
35

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
42
    {"const", CONST_UPDATE},  /* deprecated form, remove in 2018 */
    {"constantshow", CONST_UPDATE},
Chris Allegretta's avatar
Chris Allegretta committed
43
#ifndef DISABLE_WRAPJUSTIFY
44
    {"fill", 0},
Chris Allegretta's avatar
Chris Allegretta committed
45
#endif
46
47
#ifndef DISABLE_HISTORIES
    {"historylog", HISTORYLOG},
48
#endif
49
    {"morespace", MORE_SPACE},
50
#ifndef DISABLE_MOUSE
51
    {"mouse", USE_MOUSE},
52
#endif
53
#ifndef DISABLE_MULTIBUFFER
54
55
56
    {"multibuffer", MULTIBUFFER},
#endif
    {"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
64
#endif
#ifndef DISABLE_HISTORIES
65
66
    {"poslog", POS_HISTORY},  /* deprecated form, remove in 2018 */
    {"positionlog", POS_HISTORY},
67
#endif
Chris Allegretta's avatar
Chris Allegretta committed
68
    {"preserve", PRESERVE},
69
#ifndef DISABLE_JUSTIFY
70
    {"punct", 0},
71
72
    {"quotestr", 0},
#endif
73
    {"rebinddelete", REBIND_DELETE},
74
    {"rebindkeypad", REBIND_KEYPAD},
75
76
77
#ifdef HAVE_REGEX_H
    {"regexp", USE_REGEXP},
#endif
78
#ifndef DISABLE_SPELLER
79
    {"speller", 0},
80
81
82
#endif
    {"suspend", SUSPEND},
    {"tabsize", 0},
83
    {"tempfile", TEMP_FILE},
84
    {"view", VIEW_MODE},
85
#ifndef NANO_TINY
86
    {"allow_insecure_backup", INSECURE_BACKUP},
87
88
89
90
91
92
    {"autoindent", AUTOINDENT},
    {"backup", BACKUP_FILE},
    {"backupdir", 0},
    {"backwards", BACKWARDS_SEARCH},
    {"casesensitive", CASE_SENSITIVE},
    {"cut", CUT_TO_END},
93
    {"justifytrim", JUSTIFY_TRIM},
94
    {"locking", LOCKING},
95
    {"matchbrackets", 0},
96
    {"noconvert", NO_CONVERT},
97
    {"quickblank", QUICK_BLANK},
98
    {"quiet", QUIET},
99
    {"showcursor", SHOW_CURSOR},
100
101
    {"smarthome", SMART_HOME},
    {"smooth", SMOOTH_SCROLL},
102
    {"softwrap", SOFTWRAP},
103
    {"tabstospaces", TABS_TO_SPACES},
104
    {"unix", MAKE_IT_UNIX},
105
    {"whitespace", 0},
106
    {"wordbounds", WORD_BOUNDS},
107
    {"wordchars", 0},
108
109
110
111
112
113
#endif
#ifndef DISABLE_COLOR
    {"titlecolor", 0},
    {"statuscolor", 0},
    {"keycolor", 0},
    {"functioncolor", 0},
114
#endif
115
    {NULL, 0}
116
};
117

118
static bool errors = FALSE;
119
	/* Whether we got any errors while parsing an rcfile. */
120
static size_t lineno = 0;
121
	/* If we did, the line number where the last error occurred. */
122
static char *nanorc = NULL;
123
	/* The path to the rcfile we're parsing. */
124
#ifndef DISABLE_COLOR
125
126
127
static bool opensyntax = FALSE;
	/* Whether we're allowed to add to the last syntax.  When a file ends,
	 * or when a new syntax command is seen, this bool becomes FALSE. */
128
static syntaxtype *live_syntax;
129
	/* The syntax that is currently being parsed. */
130
static colortype *lastcolor = NULL;
131
132
	/* The end of the color list for the current syntax. */
#endif
133

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
134
135
136
/* 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
137
void rcfile_error(const char *msg, ...)
138
139
140
{
    va_list ap;

141
142
143
    if (ISSET(QUIET))
	return;

144
    fprintf(stderr, "\n");
145
146
    if (lineno > 0) {
	errors = TRUE;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
147
	fprintf(stderr, _("Error in %s on line %lu: "), nanorc, (unsigned long)lineno);
148
    }
Chris Allegretta's avatar
Chris Allegretta committed
149

150
    va_start(ap, msg);
151
    vfprintf(stderr, _(msg), ap);
152
    va_end(ap);
153
154

    fprintf(stderr, "\n");
155
}
156
#endif /* !DISABLE_NANORC */
157

158
#if !defined(DISABLE_NANORC) || !defined(DISABLE_HISTORIES)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
159
160
161
/* 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
162
char *parse_next_word(char *ptr)
163
{
164
    while (!isblank(*ptr) && *ptr != '\0')
165
166
167
	ptr++;

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

170
171
    /* Null-terminate and advance ptr. */
    *ptr++ = '\0';
172

173
    while (isblank(*ptr))
174
175
176
177
	ptr++;

    return ptr;
}
178
#endif /* !DISABLE_NANORC || !DISABLE_HISTORIES */
179

180
#ifndef DISABLE_NANORC
181
182
183
184
/* 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
185
186
char *parse_argument(char *ptr)
{
187
    const char *ptr_save = ptr;
Chris Allegretta's avatar
Chris Allegretta committed
188
189
190
191
192
193
194
195
196
197
198
    char *last_quote = NULL;

    assert(ptr != NULL);

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

    do {
	ptr++;
	if (*ptr == '"')
	    last_quote = ptr;
199
    } while (*ptr != '\0');
Chris Allegretta's avatar
Chris Allegretta committed
200
201
202
203
204
205

    if (last_quote == NULL) {
	if (*ptr == '\0')
	    ptr = NULL;
	else
	    *ptr++ = '\0';
206
	rcfile_error(N_("Argument '%s' has an unterminated \""), ptr_save);
Chris Allegretta's avatar
Chris Allegretta committed
207
208
209
210
211
    } else {
	*last_quote = '\0';
	ptr = last_quote + 1;
    }
    if (ptr != NULL)
212
	while (isblank(*ptr))
Chris Allegretta's avatar
Chris Allegretta committed
213
214
215
216
	    ptr++;
    return ptr;
}

217
#ifndef DISABLE_COLOR
218
219
/* Pass over the current regex string in the line starting at ptr,
 * null-terminate it, and return a pointer to the /next/ word. */
220
221
char *parse_next_regex(char *ptr)
{
222
223
    assert(ptr != NULL);

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

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

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
238
    /* Null-terminate and advance ptr. */
239
240
    *ptr++ = '\0';

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

    return ptr;
}

247
/* Compile the regular expression regex to see if it's valid.  Return
248
 * TRUE if it is, and FALSE otherwise. */
249
bool nregcomp(const char *regex, int compile_flags)
250
{
251
    regex_t preg;
252
    const char *r = fixbounds(regex);
253
    int rc = regcomp(&preg, r, compile_flags);
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"), r, str);
261
262
	free(str);
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
263

264
    regfree(&preg);
265
    return (rc == 0);
266
267
}

268
269
/* Parse the next syntax name and its possible extension regexes from the
 * line at ptr, and add it to the global linked list of color syntaxes. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
270
void parse_syntax(char *ptr)
271
{
272
273
    char *nameptr;
	/* A pointer to what should be the name of the syntax. */
274

275
276
    opensyntax = FALSE;

277
    assert(ptr != NULL);
278

279
280
281
    /* Check that the syntax name is not empty. */
    if (*ptr == '\0' || (*ptr == '"' &&
			(*(ptr + 1) == '\0' || *(ptr + 1) == '"'))) {
282
	rcfile_error(N_("Missing syntax name"));
283
	return;
284
    }
285

286
287
288
289
290
291
    nameptr = ++ptr;
    ptr = parse_next_word(ptr);

    /* Check that the name starts and ends with a double quote. */
    if (*(nameptr - 1) != '\x22' || nameptr[strlen(nameptr) - 1] != '\x22') {
	rcfile_error(N_("A syntax name must be quoted"));
292
	return;
293
    }
294

295
296
    /* Strip the end quote. */
    nameptr[strlen(nameptr) - 1] = '\0';
297

298
299
300
301
302
303
    /* Redefining the "none" syntax is not allowed. */
    if (strcmp(nameptr, "none") == 0) {
	rcfile_error(N_("The \"none\" syntax is reserved"));
	return;
    }

304
    /* Initialize a new syntax struct. */
305
306
307
308
309
310
311
    live_syntax = (syntaxtype *)nmalloc(sizeof(syntaxtype));
    live_syntax->name = mallocstrcpy(NULL, nameptr);
    live_syntax->extensions = NULL;
    live_syntax->headers = NULL;
    live_syntax->magics = NULL;
    live_syntax->linter = NULL;
    live_syntax->formatter = NULL;
312
    live_syntax->comment = NULL;
313
    live_syntax->color = NULL;
314
    lastcolor = NULL;
315
    live_syntax->nmultis = 0;
316
317

    /* Hook the new syntax in at the top of the list. */
318
319
    live_syntax->next = syntaxes;
    syntaxes = live_syntax;
320

321
322
    opensyntax = TRUE;

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

327
    /* The default syntax should have no associated extensions. */
328
    if (strcmp(live_syntax->name, "default") == 0 && *ptr != '\0') {
329
	rcfile_error(
330
		N_("The \"default\" syntax does not accept extensions"));
331
332
333
	return;
    }

334
335
    /* If there seem to be extension regexes, pick them up. */
    if (*ptr != '\0')
336
	grab_and_store("extension", ptr, &live_syntax->extensions);
337
}
338
#endif /* !DISABLE_COLOR */
339

340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* Check whether the given executable function is "universal" (meaning
 * any horizontal movement or deletion) and thus is present in almost
 * all menus. */
bool is_universal(void (*func))
{
    if (func == do_left || func == do_right ||
	func == do_home || func == do_end ||
	func == do_prev_word_void || func == do_next_word_void ||
	func == do_verbatim_input || func == do_cut_text_void ||
	func == do_delete || func == do_backspace ||
	func == do_tab || func == do_enter)
	return TRUE;
    else
	return FALSE;
}

356
/* Bind or unbind a key combo, to or from a function. */
357
void parse_binding(char *ptr, bool dobind)
358
359
{
    char *keyptr = NULL, *keycopy = NULL, *funcptr = NULL, *menuptr = NULL;
360
    sc *s, *newsc = NULL;
361
    int menu;
362
363
364

    assert(ptr != NULL);

365
366
367
368
#ifdef DEBUG
    fprintf(stderr, "Starting the rebinding code...\n");
#endif

369
370
371
372
373
374
375
376
    if (*ptr == '\0') {
	rcfile_error(N_("Missing key name"));
	return;
    }

    keyptr = ptr;
    ptr = parse_next_word(ptr);
    keycopy = mallocstrcpy(NULL, keyptr);
377
378
379

    if (strlen(keycopy) < 2) {
	rcfile_error(N_("Key name is too short"));
Benno Schulenberg's avatar
Benno Schulenberg committed
380
	goto free_copy;
381
382
383
384
385
386
387
388
389
390
    }

    /* 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"));
Benno Schulenberg's avatar
Benno Schulenberg committed
391
	    goto free_copy;
392
393
	}
    }
394

395
396
397
398
399
    /* Allow the codes for Insert and Delete to be rebound, but apart
     * from those two only Control, Meta and Function sequences. */
    if (!strcasecmp(keycopy, "Ins") || !strcasecmp(keycopy, "Del"))
	keycopy[1] = tolower(keycopy[1]);
    else if (keycopy[0] != '^' && keycopy[0] != 'M' && keycopy[0] != 'F') {
400
	rcfile_error(N_("Key name must begin with \"^\", \"M\", or \"F\""));
Benno Schulenberg's avatar
Benno Schulenberg committed
401
	goto free_copy;
402
    } else if ((keycopy[0] == 'M' && keycopy[1] != '-') ||
403
404
		(keycopy[0] == '^' && ((keycopy[1] < 'A' || keycopy[1] > 'z') ||
		keycopy[1] == '[' || keycopy[1] == '`' ||
405
406
407
		(strlen(keycopy) > 2 && strcmp(keycopy, "^Space") != 0))) ||
		(strlen(keycopy) > 3 && strcmp(keycopy, "^Space") != 0 &&
		strcmp(keycopy, "M-Space") != 0)) {
408
409
	rcfile_error(N_("Key name %s is invalid"), keycopy);
	goto free_copy;
410
411
    }

412
413
414
    if (dobind) {
	funcptr = ptr;
	ptr = parse_next_word(ptr);
415

416
	if (funcptr[0] == '\0') {
417
	    rcfile_error(N_("Must specify a function to bind the key to"));
Benno Schulenberg's avatar
Benno Schulenberg committed
418
	    goto free_copy;
419
	}
420
421
422
423
424
    }

    menuptr = ptr;
    ptr = parse_next_word(ptr);

425
    if (menuptr[0] == '\0') {
426
	/* TRANSLATORS: Do not translate the word "all". */
427
	rcfile_error(N_("Must specify a menu (or \"all\") in which to bind/unbind the key"));
Benno Schulenberg's avatar
Benno Schulenberg committed
428
	goto free_copy;
429
    }
430
431

    if (dobind) {
432
	newsc = strtosc(funcptr);
433
	if (newsc == NULL) {
434
	    rcfile_error(N_("Cannot map name \"%s\" to a function"), funcptr);
Benno Schulenberg's avatar
Benno Schulenberg committed
435
	    goto free_copy;
436
	}
437
438
    }

439
440
441
    menu = strtomenu(menuptr);
    if (menu < 1) {
	rcfile_error(N_("Cannot map name \"%s\" to a menu"), menuptr);
Benno Schulenberg's avatar
Benno Schulenberg committed
442
	goto free_copy;
443
444
    }

445
#ifdef DEBUG
446
    if (dobind)
447
448
	fprintf(stderr, "newsc address is now %ld, assigned func = %ld, menu = %x\n",
	    (long)&newsc, (long)newsc->scfunc, menu);
449
    else
450
	fprintf(stderr, "unbinding \"%s\" from menu %x\n", keycopy, menu);
451
452
#endif

453
    if (dobind) {
454
455
456
457
458
459
460
461
	subnfunc *f;
	int mask = 0;

	/* Tally up the menus where the function exists. */
	for (f = allfuncs; f != NULL; f = f->next)
	    if (f->scfunc == newsc->scfunc)
		mask = mask | f->menus;

462
#ifndef NANO_TINY
463
464
465
	/* Handle the special case of the toggles. */
	if (newsc->scfunc == do_toggle_void)
	    mask = MMAIN;
466
#endif
467

468
469
470
471
472
473
474
475
476
	/* Now limit the given menu to those where the function exists. */
	if (is_universal(newsc->scfunc))
	    menu = menu & MMOST;
	else
	    menu = menu & mask;

	if (!menu) {
	    rcfile_error(N_("Function '%s' does not exist in menu '%s'"), funcptr, menuptr);
	    free(newsc);
Benno Schulenberg's avatar
Benno Schulenberg committed
477
	    goto free_copy;
478
479
	}

480
	newsc->menus = menu;
481
	assign_keyinfo(newsc, keycopy);
482

483
	/* Do not allow rebinding a frequent escape-sequence starter: Esc [. */
484
	if (newsc->meta && newsc->keycode == 91) {
485
486
	    rcfile_error(N_("Sorry, keystroke \"%s\" may not be rebound"), newsc->keystr);
	    free(newsc);
Benno Schulenberg's avatar
Benno Schulenberg committed
487
	    goto free_copy;
488
	}
489
490
#ifdef DEBUG
	fprintf(stderr, "s->keystr = \"%s\"\n", newsc->keystr);
491
	fprintf(stderr, "s->keycode = \"%d\"\n", newsc->keycode);
492
#endif
493
    }
494

495
    /* Now find and delete any existing same shortcut in the menu(s). */
496
    for (s = sclist; s != NULL; s = s->next) {
497
	if ((s->menus & menu) && !strcmp(s->keystr, keycopy)) {
498
#ifdef DEBUG
499
	    fprintf(stderr, "deleting entry from among menus %x\n", s->menus);
500
#endif
501
	    s->menus &= ~menu;
502
503
	}
    }
504
505

    if (dobind) {
506
#ifndef NANO_TINY
507
508
509
	/* If this is a toggle, copy its sequence number. */
	if (newsc->scfunc == do_toggle_void) {
	    for (s = sclist; s != NULL; s = s->next)
510
		if (s->scfunc == do_toggle_void && s->toggle == newsc->toggle)
511
512
513
		    newsc->ordinal = s->ordinal;
	} else
	    newsc->ordinal = 0;
514
#endif
515
516
517
	/* Add the new shortcut at the start of the list. */
	newsc->next = sclist;
	sclist = newsc;
Benno Schulenberg's avatar
Benno Schulenberg committed
518
519
520
521
522
	return;
    }

  free_copy:
    free(keycopy);
523
524
}

525
#ifndef DISABLE_COLOR
526
527
/* Read and parse one included syntax file. */
static void parse_one_include(char *file)
528
529
530
531
{
    struct stat rcinfo;
    FILE *rcstream;

532
533
534
    /* 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). */
535
536

    /* Don't open directories, character files, or block files. */
537
    if (stat(file, &rcinfo) != -1) {
538
539
540
541
	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") :
542
		_("\"%s\" is a device file"), file);
543
544
545
546
	}
    }

    /* Open the new syntax file. */
547
548
    if ((rcstream = fopen(file, "rb")) == NULL) {
	rcfile_error(_("Error reading %s: %s"), file,
549
		strerror(errno));
550
	return;
551
552
553
554
    }

    /* 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. */
555
    nanorc = file;
556
557
    lineno = 0;

558
#ifdef DEBUG
559
    fprintf(stderr, "Parsing file \"%s\"\n", file);
560
561
#endif

562
    parse_rcfile(rcstream, TRUE);
563
564
}

565
566
/* Expand globs in the passed name, and parse the resultant files. */
void parse_includes(char *ptr)
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
{
    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)
582
583
	    parse_one_include(files.gl_pathv[i]);
    } else
584
585
	rcfile_error(_("Error expanding %s: %s"), option,
		strerror(errno));
586

587
588
589
    globfree(&files);
    free(expanded);

590
    /* We're done with the included file(s).  Restore the original
591
592
593
594
595
     * filename and line number position. */
    nanorc = nanorc_save;
    lineno = lineno_save;
}

596
597
/* Return the short value corresponding to the color named in colorname,
 * and set bright to TRUE if that color is bright. */
598
short color_to_short(const char *colorname, bool *bright)
599
600
601
602
603
604
605
606
607
{
    assert(colorname != NULL && bright != NULL);

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

    if (strcasecmp(colorname, "green") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
608
	return COLOR_GREEN;
609
    else if (strcasecmp(colorname, "red") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
610
	return COLOR_RED;
611
    else if (strcasecmp(colorname, "blue") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
612
	return COLOR_BLUE;
613
    else if (strcasecmp(colorname, "white") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
614
	return COLOR_WHITE;
615
    else if (strcasecmp(colorname, "yellow") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
616
	return COLOR_YELLOW;
617
    else if (strcasecmp(colorname, "cyan") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
618
	return COLOR_CYAN;
619
    else if (strcasecmp(colorname, "magenta") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
620
	return COLOR_MAGENTA;
621
    else if (strcasecmp(colorname, "black") == 0)
Benno Schulenberg's avatar
Benno Schulenberg committed
622
623
624
	return COLOR_BLACK;

    rcfile_error(N_("Color \"%s\" not understood.\n"
625
626
627
628
		"Valid colors are \"green\", \"red\", \"blue\",\n"
		"\"white\", \"yellow\", \"cyan\", \"magenta\" and\n"
		"\"black\", with the optional prefix \"bright\"\n"
		"for foreground colors."), colorname);
Benno Schulenberg's avatar
Benno Schulenberg committed
629
    return -1;
630
631
}

632
/* Parse the color string in the line at ptr, and add it to the current
633
634
635
 * file's associated colors.  rex_flags are the regex compilation flags
 * to use, excluding or including REG_ICASE for case (in)sensitivity. */
void parse_colors(char *ptr, int rex_flags)
636
{
637
    short fg, bg;
638
    bool bright = FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
639
    char *fgstr;
640

641
    assert(ptr != NULL);
642

643
    if (!opensyntax) {
644
	rcfile_error(
645
646
		N_("A '%s' command requires a preceding 'syntax' command"),
		"color");
647
648
649
	return;
    }

650
    if (*ptr == '\0') {
651
	rcfile_error(N_("Missing color name"));
652
	return;
653
654
    }

655
656
    fgstr = ptr;
    ptr = parse_next_word(ptr);
657
658
    if (!parse_color_names(fgstr, &fg, &bg, &bright))
	return;
659

660
    if (*ptr == '\0') {
661
	rcfile_error(N_("Missing regex string after '%s' command"), "color");
662
663
664
	return;
    }

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
665
    /* Now for the fun part.  Start adding regexes to individual strings
666
667
     * in the colorstrings array, woo! */
    while (ptr != NULL && *ptr != '\0') {
668
	colortype *newcolor = NULL;
669
	    /* The container for a color plus its regexes. */
Benno Schulenberg's avatar
Benno Schulenberg committed
670
671
	bool goodstart;
	    /* Whether the start expression was valid. */
672
	bool expectend = FALSE;
Benno Schulenberg's avatar
Benno Schulenberg committed
673
	    /* Whether to expect an end= line. */
674

675
	if (strncasecmp(ptr, "start=", 6) == 0) {
676
	    ptr += 6;
677
	    expectend = TRUE;
678
679
680
	}

	if (*ptr != '"') {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
681
682
	    rcfile_error(
		N_("Regex strings must begin and end with a \" character"));
683
	    ptr = parse_next_regex(ptr);
684
685
	    continue;
	}
686

687
	fgstr = ++ptr;
688
	ptr = parse_next_regex(ptr);
689
690
691
	if (ptr == NULL)
	    break;

692
	goodstart = nregcomp(fgstr, rex_flags);
Benno Schulenberg's avatar
Benno Schulenberg committed
693
694
695
696

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

699
700
701
	    newcolor->fg = fg;
	    newcolor->bg = bg;
	    newcolor->bright = bright;
702
	    newcolor->rex_flags = rex_flags;
703
704
705
706

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

707
	    newcolor->end_regex = NULL;
708
	    newcolor->end = NULL;
709

710
	    newcolor->next = NULL;
711

Chris Allegretta's avatar
Chris Allegretta committed
712
#ifdef DEBUG
Benno Schulenberg's avatar
Benno Schulenberg committed
713
	    fprintf(stderr, "Adding an entry for fg %hd, bg %hd\n", fg, bg);
Chris Allegretta's avatar
Chris Allegretta committed
714
#endif
715
	    if (lastcolor == NULL)
Benno Schulenberg's avatar
Benno Schulenberg committed
716
		live_syntax->color = newcolor;
717
	    else
718
		lastcolor->next = newcolor;
719

720
	    lastcolor = newcolor;
Benno Schulenberg's avatar
Benno Schulenberg committed
721
	}
Chris Allegretta's avatar
Chris Allegretta committed
722

723
724
	if (!expectend)
	    continue;
725

726
727
728
729
	if (ptr == NULL || strncasecmp(ptr, "end=", 4) != 0) {
	    rcfile_error(N_("\"start=\" requires a corresponding \"end=\""));
	    return;
	}
730

731
732
733
734
735
	ptr += 4;
	if (*ptr != '"') {
	    rcfile_error(N_("Regex strings must begin and end with a \" character"));
	    continue;
	}
736

737
738
739
	fgstr = ++ptr;
	ptr = parse_next_regex(ptr);
	if (ptr == NULL)
740
	    break;
741

742
743
	/* If the start regex was invalid, skip past the end regex
	 * to stay in sync. */
Benno Schulenberg's avatar
Benno Schulenberg committed
744
	if (!goodstart)
745
746
747
	    continue;

	/* If it's valid, save the ending regex string. */
748
	if (nregcomp(fgstr, rex_flags))
749
750
751
752
753
	    newcolor->end_regex = mallocstrcpy(NULL, fgstr);

	/* Lame way to skip another static counter. */
	newcolor->id = live_syntax->nmultis;
	live_syntax->nmultis++;
754
    }
755
}
756

757
758
759
760
761
762
/* Parse the color name, or pair of color names, in combostr. */
bool parse_color_names(char *combostr, short *fg, short *bg, bool *bright)
{
    bool no_fgcolor = FALSE;

    if (combostr == NULL)
763
	return FALSE;
764
765
766
767
768
769
770
771
772
773
774
775
776

    if (strchr(combostr, ',') != NULL) {
	char *bgcolorname;
	strtok(combostr, ",");
	bgcolorname = strtok(NULL, ",");
	if (bgcolorname == NULL) {
	    /* If we have a background color without a foreground color,
	     * parse it properly. */
	    bgcolorname = combostr + 1;
	    no_fgcolor = TRUE;
	}
	if (strncasecmp(bgcolorname, "bright", 6) == 0) {
	    rcfile_error(N_("Background color \"%s\" cannot be bright"), bgcolorname);
777
	    return FALSE;
778
779
780
781
782
783
784
785
786
787
	}
	*bg = color_to_short(bgcolorname, bright);
    } else
	*bg = -1;

    if (!no_fgcolor) {
	*fg = color_to_short(combostr, bright);

	/* Don't try to parse screwed-up foreground colors. */
	if (*fg == -1)
788
	    return FALSE;
789
790
791
    } else
	*fg = -1;

792
    return TRUE;
793
794
}

795
796
/* Read regex strings enclosed in double quotes from the line pointed at
 * by ptr, and store them quoteless in the passed storage place. */
797
void grab_and_store(const char *kind, char *ptr, regexlisttype **storage)
798
{
799
    regexlisttype *lastthing;
800

801
    if (!opensyntax) {
802
	rcfile_error(
803
		N_("A '%s' command requires a preceding 'syntax' command"), kind);
804
805
806
	return;
    }

807
808
    /* The default syntax doesn't take any file matching stuff. */
    if (strcmp(live_syntax->name, "default") == 0 && *ptr != '\0') {
809
	rcfile_error(
810
		N_("The \"default\" syntax does not accept '%s' regexes"), kind);
811
812
813
814
	return;
    }

    if (*ptr == '\0') {
815
	rcfile_error(N_("Missing regex string after '%s' command"), kind);
816
817
818
	return;
    }

819
820
821
822
823
824
    lastthing = *storage;

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

825
    /* Now gather any valid regexes and add them to the linked list. */
826
827
    while (*ptr != '\0') {
	const char *regexstring;
828
	regexlisttype *newthing;
829
830
831
832

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

836
	regexstring = ++ptr;
837
838
	ptr = parse_next_regex(ptr);
	if (ptr == NULL)
839
	    return;
840

841
	/* If the regex string is malformed, skip it. */
842
	if (!nregcomp(regexstring, NANO_REG_EXTENDED | REG_NOSUB))
843
	    continue;
844

845
846
847
848
	/* Copy the regex into a struct, and hook this in at the end. */
	newthing = (regexlisttype *)nmalloc(sizeof(regexlisttype));
	newthing->full_regex = mallocstrcpy(NULL, regexstring);
	newthing->next = NULL;
849

850
851
852
853
	if (lastthing == NULL)
	    *storage = newthing;
	else
	    lastthing->next = newthing;
854

855
	lastthing = newthing;
856
857
    }
}
858

859
860
/* Parse and store the name given after a linter/formatter command. */
void pick_up_name(const char *kind, char *ptr, char **storage)
861
862
863
{
    assert(ptr != NULL);

864
    if (!opensyntax) {
865
	rcfile_error(
866
		N_("A '%s' command requires a preceding 'syntax' command"), kind);
867
868
869
870
	return;
    }

    if (*ptr == '\0') {
871
	rcfile_error(N_("Missing command after '%s'"), kind);
872
873
874
	return;
    }

875
    free(*storage);
876

877
    /* Allow unsetting the command by using an empty string. */
878
    if (!strcmp(ptr, "\"\""))
879
	*storage = NULL;
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
    else if (*ptr == '"') {
	*storage = mallocstrcpy(NULL, ++ptr);
	char* q = *storage;
	char* p = *storage;
	/* Snip out the backslashes of escaped characters. */
	while (*p != '"') {
	    if (*p == '\0') {
		rcfile_error(N_("Argument of '%s' lacks closing \""), kind);
		free(*storage);
		*storage = NULL;
		return;
	    } else if (*p == '\\' && *(p + 1) != '\0') {
		p++;
	    }
	    *q++ = *p++;
	}
	*q = '\0';
    }
898
    else
899
	*storage = mallocstrcpy(NULL, ptr);
900
}
901
#endif /* !DISABLE_COLOR */
902

903
/* Check whether the user has unmapped every shortcut for a
904
 * sequence we consider 'vital', like the exit function. */
905
906
907
908
909
static void check_vitals_mapped(void)
{
    subnfunc *f;
    int v;
#define VITALS 5
910
    void (*vitals[VITALS])(void) = { do_exit, do_exit, do_cancel, do_cancel, do_cancel };
911
912
913
    int inmenus[VITALS] = { MMAIN, MHELP, MWHEREIS, MREPLACE, MGOTOLINE };

    for  (v = 0; v < VITALS; v++) {
914
915
916
917
918
	for (f = allfuncs; f != NULL; f = f->next) {
	    if (f->scfunc == vitals[v] && f->menus & inmenus[v]) {
		const sc *s = first_sc_for(inmenus[v], f->scfunc);
		if (!s) {
		    fprintf(stderr, _("Fatal error: no keys mapped for function "
919
					"\"%s\".  Exiting.\n"), f->desc);
920
		    fprintf(stderr, _("If needed, use nano with the -I option "
921
922
					"to adjust your nanorc settings.\n"));
		    exit(1);
923
924
925
926
		}
		break;
	    }
	}
927
928
929
    }
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
930
931
/* Parse the rcfile, once it has been opened successfully at rcstream,
 * and close it afterwards.  If syntax_only is TRUE, only allow the file
932
 * to contain color syntax commands. */
933
void parse_rcfile(FILE *rcstream
934
#ifndef DISABLE_COLOR
935
936
937
	, bool syntax_only
#endif
	)
938
{
939
940
    char *buf = NULL;
    ssize_t len;
941
    size_t n = 0;
942
943
944

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
948
	/* Ignore the newline. */
949
950
	if (buf[len - 1] == '\n')
	    buf[len - 1] = '\0';
951
952
953

	lineno++;
	ptr = buf;
954
	while (isblank(*ptr))
955
956
	    ptr++;

957
958
959
	/* If we have a blank line or a comment, skip to the next
	 * line. */
	if (*ptr == '\0' || *ptr == '#')
960
961
	    continue;

962
	/* Otherwise, skip to the next space. */
963
964
965
	keyword = ptr;
	ptr = parse_next_word(ptr);

966
#ifndef DISABLE_COLOR
967
968
	/* Handle extending first... */
	if (strcasecmp(keyword, "extendsyntax") == 0) {
969
	    syntaxtype *sint;
970
971
972
	    char *syntaxname = ptr;

	    ptr = parse_next_word(ptr);
973
974
975

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

978
979
980
	    if (sint == NULL) {
		rcfile_error(N_("Could not find syntax \"%s\" to extend"),
				syntaxname);
981
		opensyntax = FALSE;
982
983
		continue;
	    }
984
985
986
987

	    live_syntax = sint;
	    opensyntax = TRUE;

988
	    /* Refind the tail of the color list for this syntax. */
989
990
991
992
	    lastcolor = sint->color;
	    if (lastcolor != NULL)
		while (lastcolor->next != NULL)
		    lastcolor = lastcolor->next;
993

994
995
	    keyword = ptr;
	    ptr = parse_next_word(ptr);
996
997
	}

998
	/* Try to parse the keyword. */
999
	if (strcasecmp(keyword, "syntax") == 0) {
1000
	    if (opensyntax && lastcolor == NULL)
1001
		rcfile_error(N_("Syntax \"%s\" has no color commands"),
1002
				live_syntax->name);
Chris Allegretta's avatar
Chris Allegretta committed
1003
	    parse_syntax(ptr);
1004
	}
1005
1006
	else if (strcasecmp(keyword, "header") == 0)
	    grab_and_store("header", ptr, &live_syntax->headers);
1007
	else if (strcasecmp(keyword, "magic") == 0)
1008
#ifdef HAVE_LIBMAGIC
1009
	    grab_and_store("magic", ptr, &live_syntax->magics);
1010
1011
#else
	    ;
1012
1013
1014
1015
1016
1017
#endif
	else if (strcasecmp(keyword, "comment") == 0)
#ifdef ENABLE_COMMENT
	    pick_up_name("comment", ptr, &live_syntax->comment);
#else
	    ;
1018
#endif
1019
	else if (strcasecmp(keyword, "color") == 0)
1020
	    parse_colors(ptr, NANO_REG_EXTENDED);
1021
	else if (strcasecmp(keyword, "icolor") == 0)
1022
	    parse_colors(ptr, NANO_REG_EXTENDED | REG_ICASE);
1023
	else if (strcasecmp(keyword, "linter") == 0)
1024
	    pick_up_name("linter", ptr, &live_syntax->linter);
1025
	else if (strcasecmp(keyword, "formatter") == 0)
1026
#ifndef DISABLE_SPELLER
1027
	    pick_up_name("formatter", ptr, &live_syntax->formatter);
1028
1029
1030
#else
	    ;
#endif
1031
1032
1033
1034
1035
1036
	else if (syntax_only)
	    rcfile_error(N_("Command \"%s\" not allowed in included file"),
					keyword);
	else if (strcasecmp(keyword, "include") == 0)
	    parse_includes(ptr);
	else
1037
#endif /* !DISABLE_COLOR */
1038
1039
1040
1041
	if (strcasecmp(keyword, "set") == 0)
	    set = 1;
	else if (strcasecmp(keyword, "unset") == 0)
	    set = -1;
1042
	else if (strcasecmp(keyword, "bind") == 0)
1043
	    parse_binding(ptr, TRUE);
1044
	else if (strcasecmp(keyword, "unbind") == 0)
1045
	    parse_binding(ptr, FALSE);
1046
	else
1047
	    rcfile_error(N_("Command \"%s\" not understood"), keyword);
1048

1049
#ifndef DISABLE_COLOR
1050
1051
	/* If a syntax was extended, it stops at the end of the command. */
	if (live_syntax != syntaxes)
1052
	    opensyntax = FALSE;
1053
1054
#endif

1055
1056
1057
1058
	if (set == 0)
	    continue;

	if (*ptr == '\0') {
1059
	    rcfile_error(N_("Missing option"));
1060
1061
1062
1063
1064
1065
	    continue;
	}

	option = ptr;
	ptr = parse_next_word(ptr);

1066
	/* Find the just read name among the existing options. */
1067
	for (i = 0; rcopts[i].name != NULL; i++) {
1068
1069
1070
1071
1072
1073
1074
1075
1076
	    if (strcasecmp(option, rcopts[i].name) == 0)
		break;
	}

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

1077
#ifdef DEBUG
1078
1079
	fprintf(stderr, "    Option name = \"%s\"\n", rcopts[i].name);
	fprintf(stderr, "    Flag = %ld\n", rcopts[i].flag);
1080
#endif
1081
1082
	/* First handle unsetting. */
	if (set == -1) {
1083
	    if (rcopts[i].flag != 0)
1084
		UNSET(rcopts[i].flag);
1085
	    else
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
		rcfile_error(N_("Cannot unset option \"%s\""), rcopts[i].name);
	    continue;
	}

	/* If the option has a flag, it doesn't take an argument. */
	if (rcopts[i].flag != 0) {
	    SET(rcopts[i].flag);
	    continue;
	}

	/* The option doesn't have a flag, so it takes an argument. */
	if (*ptr == '\0') {
	    rcfile_error(N_("Option \"%s\" requires an argument"),
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1099
				rcopts[i].name);
1100
1101
1102
1103
1104
1105
1106
1107
1108
	    continue;
	}

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

	option = mallocstrcpy(NULL, option);
Chris Allegretta's avatar
Chris Allegretta committed
1109
#ifdef DEBUG
1110
	fprintf(stderr, "    Option argument = \"%s\"\n", option);
Chris Allegretta's avatar
Chris Allegretta committed
1111
#endif
1112
1113
1114
1115
1116
	/* Make sure the option argument is a valid multibyte string. */
	if (!is_valid_mbstring(option)) {
	    rcfile_error(N_("Option is not a valid multibyte string"));
	    continue;
	}
1117

1118
#ifndef DISABLE_COLOR
1119
1120
1121
1122
1123
1124
1125
1126
1127
	if (strcasecmp(rcopts[i].name, "titlecolor") == 0)
	    specified_color_combo[TITLE_BAR] = option;
	else if (strcasecmp(rcopts[i].name, "statuscolor") == 0)
	    specified_color_combo[STATUS_BAR] = option;
	else if (strcasecmp(rcopts[i].name, "keycolor") == 0)
	    specified_color_combo[KEY_COMBO] = option;
	else if (strcasecmp(rcopts[i].name, "functioncolor") == 0)
	    specified_color_combo[FUNCTION_TAG] = option;
	else
1128
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1129
#ifndef DISABLE_OPERATINGDIR
1130
1131
1132
	if (strcasecmp(rcopts[i].name, "operatingdir") == 0)
	    operating_dir = option;
	else
Chris Allegretta's avatar
Chris Allegretta committed
1133
#endif
1134
#ifndef DISABLE_WRAPJUSTIFY
1135
1136
1137
1138
1139
1140
1141
1142
	if (strcasecmp(rcopts[i].name, "fill") == 0) {
	    if (!parse_num(option, &wrap_at)) {
		rcfile_error(N_("Requested fill size \"%s\" is invalid"),
				option);
		wrap_at = -CHARS_FROM_EOL;
	    } else
		free(option);
	} else
Chris Allegretta's avatar
Chris Allegretta committed
1143
#endif
1144
#ifndef NANO_TINY
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
	if (strcasecmp(rcopts[i].name, "matchbrackets") == 0) {
	    matchbrackets = option;
	    if (has_blank_mbchars(matchbrackets)) {
		rcfile_error(N_("Non-blank characters required"));
		free(matchbrackets);
		matchbrackets = NULL;
	    }
	} else if (strcasecmp(rcopts[i].name, "whitespace") == 0) {
	    whitespace = option;
	    if (mbstrlen(whitespace) != 2 || strlenpt(whitespace) != 2) {
		rcfile_error(N_("Two single-column characters required"));
		free(whitespace);
		whitespace = NULL;
	    } else {
		whitespace_len[0] = parse_mbchar(whitespace, NULL, NULL);
		whitespace_len[1] = parse_mbchar(whitespace +
1161
					whitespace_len[0], NULL, NULL);
1162
1163
	    }
	} else
1164
#endif
1165
#ifndef DISABLE_JUSTIFY
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
	if (strcasecmp(rcopts[i].name, "punct") == 0) {
	    punct = option;
	    if (has_blank_mbchars(punct)) {
		rcfile_error(N_("Non-blank characters required"));
		free(punct);
		punct = NULL;
	    }
	} else if (strcasecmp(rcopts[i].name, "brackets") == 0) {
	    brackets = option;
	    if (has_blank_mbchars(brackets)) {
		rcfile_error(N_("Non-blank characters required"));
		free(brackets);
		brackets = NULL;
	    }
	} else if (strcasecmp(rcopts[i].name, "quotestr") == 0)
	    quotestr = option;
	else
1183
#endif
1184
#ifndef NANO_TINY
1185
1186
1187
	if (strcasecmp(rcopts[i].name, "backupdir") == 0)
	    backup_dir = option;
	else
1188
1189
1190
	if (strcasecmp(rcopts[i].name, "wordchars") == 0)
	    word_chars = option;
	else
1191
#endif
1192
#ifndef DISABLE_SPELLER
1193
1194
1195
	if (strcasecmp(rcopts[i].name, "speller") == 0)
	    alt_speller = option;
	else
1196
#endif
1197
1198
1199
1200
1201
1202
1203
1204
1205
	if (strcasecmp(rcopts[i].name, "tabsize") == 0) {
	    if (!parse_num(option, &tabsize) || tabsize <= 0) {
		rcfile_error(N_("Requested tab size \"%s\" is invalid"),
				option);
		tabsize = -1;
	    } else
		free(option);
	} else
	    assert(FALSE);
1206
    }
1207

1208
#ifndef DISABLE_COLOR
1209
    if (opensyntax && lastcolor == NULL)
1210
	rcfile_error(N_("Syntax \"%s\" has no color commands"),
1211
			live_syntax->name);
1212

1213
    opensyntax = FALSE;
1214
#endif
1215

Chris Allegretta's avatar
Chris Allegretta committed
1216
    free(buf);
1217
1218
    fclose(rcstream);
    lineno = 0;
1219

Chris Allegretta's avatar
Chris Allegretta committed
1220
    check_vitals_mapped();
1221
1222
1223
    return;
}

1224
/* The main rcfile function.  It tries to open the system-wide rcfile,
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1225
 * followed by the current user's rcfile. */
1226
1227
void do_rcfile(void)
{
1228
    struct stat rcinfo;
1229
1230
    FILE *rcstream;

1231
    nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc");
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241

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

1242
1243
1244
1245
#ifdef DEBUG
    fprintf(stderr, "Parsing file \"%s\"\n", nanorc);
#endif

1246
    /* Try to open the system-wide nanorc. */
1247
    rcstream = fopen(nanorc, "rb");
1248
    if (rcstream != NULL)
1249
	parse_rcfile(rcstream
1250
#ifndef DISABLE_COLOR
1251
1252
1253
		, FALSE
#endif
		);
1254

1255
#ifdef DISABLE_ROOTWRAPPING
1256
    /* We've already read SYSCONFDIR/nanorc, if it's there.  If we're
1257
1258
     * root, and --disable-wrapping-as-root is used, turn wrapping off
     * now. */
1259
1260
1261
    if (geteuid() == NANO_ROOT_UID)
	SET(NO_WRAP);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1262

1263
    get_homedir();
1264

1265
    if (homedir == NULL)
1266
	rcfile_error(N_("I can't find my home directory!  Wah!"));
1267
    else {
1268
1269
1270
1271
1272
#ifndef RCFILE_NAME
#define RCFILE_NAME ".nanorc"
#endif
	nanorc = charealloc(nanorc, strlen(homedir) + strlen(RCFILE_NAME) + 2);
	sprintf(nanorc, "%s/%s", homedir, RCFILE_NAME);
1273

1274
1275
1276
	/* 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) ||
1277
			S_ISBLK(rcinfo.st_mode))
1278
1279
1280
1281
1282
1283
1284
		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");
1285
	if (rcstream == NULL) {
1286
1287
	    /* Don't complain about the file's not existing. */
	    if (errno != ENOENT)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1288
		rcfile_error(N_("Error reading %s: %s"), nanorc,
1289
				strerror(errno));
1290
	} else
1291
	    parse_rcfile(rcstream
1292
#ifndef DISABLE_COLOR
1293
1294
1295
		, FALSE
#endif
		);
1296
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1297

1298
1299
    free(nanorc);
    nanorc = NULL;
1300

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

1309
#endif /* !DISABLE_NANORC */