rcfile.c 14.5 KB
Newer Older
1
2
/* $Id$ */
/**************************************************************************
3
 *   rcfile.c                                                             *
4
 *                                                                        *
5
 *   Copyright (C) 1999-2002 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
11
12
13
14
15
16
17
18
19
20
21
22
 *   any later version.                                                   *
 *                                                                        *
 *   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.                         *
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
 *                                                                        *
 **************************************************************************/

#include <stdlib.h>
23
#include <stdarg.h>
24
25
26
#include <string.h>
#include <stdio.h>
#include <errno.h>
Chris Allegretta's avatar
Chris Allegretta committed
27
28
#include <unistd.h>
#include <pwd.h>
29
30
31
32
33
34
35
36
37
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "config.h"
#include "proto.h"
#include "nano.h"

#ifdef ENABLE_NANORC

38
#ifdef ENABLE_NLS
39
40
41
42
43
44
45
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif

/* Static stuff for the nanorc file */
46
rcoption rcopts[] = {
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    {"regexp", USE_REGEXP},
    {"const", CONSTUPDATE},
    {"autoindent", AUTOINDENT},
    {"cut", CUT_TO_END},
    {"nofollow", FOLLOW_SYMLINKS},
    {"mouse", USE_MOUSE},
    {"operatingdir", 0},
    {"pico", PICO_MODE},
    {"tabsize", 0},
    {"fill", 0},
    {"speller", 0},
    {"tempfile", TEMP_OPT},
    {"view", VIEW_MODE},
    {"nowrap", NO_WRAP},
    {"nohelp", NO_HELP},
    {"suspend", SUSPEND},
    {"multibuffer", MULTIBUFFER},
    {"smooth", SMOOTHSCROLL},
65
    {"keypad", ALT_KEYPAD},
66
67
68
    {"noconvert", NO_CONVERT},
    {"quotestr", 0},
    {"", 0}
69
};
70

71
72
73
74
static int errors = 0;
static int lineno = 0;
static char *nanorc;

75
76
/* We have an error in some part of the rcfile; put it on stderr and
  make the user hit return to continue starting up nano */
77
78
79
80
81
void rcfile_error(char *msg, ...)
{
    va_list ap;

    fprintf(stderr, "\n");
Chris Allegretta's avatar
Chris Allegretta committed
82
83
84
    if (lineno > 0)
	fprintf(stderr, _("Error in %s on line %d: "), nanorc, lineno);

85
86
87
88
89
    va_start(ap, msg);
    vfprintf(stderr, msg, ap);
    va_end(ap);
    fprintf(stderr, _("\nPress return to continue starting nano\n"));

90
    while (getchar() != '\n');
91
92
93

}

94
/* Just print the error (one of many, perhaps) but don't abort, yet */
95
void rcfile_msg(char *msg, ...)
96
97
98
{
    va_list ap;

99
100
    if (!errors) {
	errors = 1;
101
102
103
104
105
106
107
108
109
	fprintf(stderr, "\n");
    }
    va_start(ap, msg);
    vfprintf(stderr, msg, ap);
    va_end(ap);
    fprintf(stderr, "\n");

}

110
/* Parse the next word from the string.  Returns NULL if we hit EOL */
111
112
char *parse_next_word(char *ptr)
{
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n' && *ptr != '\0')
	ptr++;

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

    /* Null terminate and advance ptr */
    *ptr++ = 0;

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

    return ptr;
}
127

128
129
char *parse_next_regex(char *ptr)
{
130
131
    while ((*ptr != '"' || (*(ptr+1) != ' ' && *(ptr+1) != '\n')) 
	   && *ptr != '\n' && *ptr != '\0')
132
133
	ptr++;

134
    if (*ptr == '\0')
135
	return NULL;
136

137
138
139
    /* Null terminate and advance ptr */
    *ptr++ = 0;

140
141
142
    while ((*ptr == ' ' || *ptr == '\t') && *ptr != '\0')
	ptr++;

143
    return ptr;
144

145
146
}

147
int colortoint(char *colorname, int *bright)
148
149
150
151
152
153
{
    int mcolor = 0;

    if (colorname == NULL)
	return -1;

154
    if (stristr(colorname, "bright")) {
155
	*bright = 1;
156
157
	colorname += 6;
    }
158

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    if (!strcasecmp(colorname, "green"))
	mcolor += COLOR_GREEN;
    else if (!strcasecmp(colorname, "red"))
	mcolor += COLOR_RED;
    else if (!strcasecmp(colorname, "blue"))
	mcolor += COLOR_BLUE;
    else if (!strcasecmp(colorname, "white"))
	mcolor += COLOR_WHITE;
    else if (!strcasecmp(colorname, "yellow"))
	mcolor += COLOR_YELLOW;
    else if (!strcasecmp(colorname, "cyan"))
	mcolor += COLOR_CYAN;
    else if (!strcasecmp(colorname, "magenta"))
	mcolor += COLOR_MAGENTA;
    else if (!strcasecmp(colorname, "black"))
	mcolor += COLOR_BLACK;
    else {
176
177
178
179
	rcfile_error(_("color %s not understood.\n"
		       "Valid colors are \"green\", \"red\", \"blue\", \n"
		       "\"white\", \"yellow\", \"cyan\", \"magenta\" and \n"
		       "\"black\", with the optional prefix \"bright\".\n"));
180
181
182
183
184
185
186
187
	exit(1);
    }

    return mcolor;
}


#ifdef ENABLE_COLOR
188
189
190
191
192
193
194
195
196
197
198
199
200
201
void parse_syntax(FILE * rcstream, char *buf, char *ptr)
{
    syntaxtype *tmpsyntax = NULL;
    char *fileregptr = NULL, *nameptr = NULL;
    exttype *exttmp = NULL;

    while (*ptr == ' ')
	ptr++;

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

    if (*ptr != '"') {
	rcfile_error(_("regex strings must begin and end with a \" character\n"));
202
	return;
203
204
205
206
207
208
209
210
    }
    ptr++;

    nameptr = ptr;
    ptr = parse_next_regex(ptr);

    if (ptr == NULL) {
	rcfile_error(_("Missing syntax name"));
211
	return;
212
213
214
215
216
217
218
219
220
221
222
223
224
    }

	if (syntaxes == NULL) {
	    syntaxes = nmalloc(sizeof(syntaxtype));
	    syntaxes->desc = NULL;
	    syntaxes->desc = mallocstrcpy(syntaxes->desc, nameptr);
	    syntaxes->color = NULL;
	    syntaxes->extensions = NULL;
	    syntaxes->next = NULL;
	    tmpsyntax = syntaxes;
#ifdef DEBUG
	    fprintf(stderr,
		    "Starting a new syntax type\n");
225
	    fprintf(stderr, "string val=%s\n", nameptr);
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#endif

	} else {
	    for (tmpsyntax = syntaxes;
		 tmpsyntax->next != NULL; tmpsyntax = tmpsyntax->next);
#ifdef DEBUG
	    fprintf(stderr, "Adding new syntax after 1st\n");
#endif

	    tmpsyntax->next = nmalloc(sizeof(syntaxtype));
	    tmpsyntax->next->desc = NULL;
	    tmpsyntax->next->desc = mallocstrcpy(tmpsyntax->next->desc, nameptr);
	    tmpsyntax->next->color = NULL;
	    tmpsyntax->next->extensions = NULL;
	    tmpsyntax->next->next = NULL;
	    tmpsyntax = tmpsyntax->next;
	}

    /* Now load in the extensions to their part of the struct */
    while (*ptr != '\n' && *ptr != '\0') {

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

	if (*ptr == '\n' || *ptr == '\0')
	    return;
	ptr++;

	fileregptr = ptr;
	ptr = parse_next_regex(ptr);

	if (tmpsyntax->extensions == NULL) {
	    tmpsyntax->extensions = nmalloc(sizeof(exttype));
	    tmpsyntax->extensions->val = NULL;
	    tmpsyntax->extensions->val = mallocstrcpy(tmpsyntax->extensions->val, fileregptr);
	    tmpsyntax->extensions->next = NULL;
	}
	else {
	    for (exttmp = tmpsyntax->extensions; exttmp->next != NULL;
		 exttmp = exttmp->next);
	    exttmp->next = nmalloc(sizeof(exttype));
	    exttmp->next->val = NULL;
	    exttmp->next->val = mallocstrcpy(exttmp->next->val, fileregptr);
	    exttmp->next->next = NULL;
	}
   }

}

275
/* Parse the color stuff into the colorstrings array */
276
void parse_colors(FILE * rcstream, char *buf, char *ptr)
277
{
278
279
    int fg, bg, bright = 0;
    int expectend = 0;		/* Do we expect an end= line? */
280
281
    char *tmp = NULL, *beginning, *fgstr, *bgstr;
    colortype *tmpcolor = NULL;
282
    syntaxtype *tmpsyntax = NULL;
283
284
285
286
287

    fgstr = ptr;
    ptr = parse_next_word(ptr);

    if (ptr == NULL) {
288
	rcfile_error(_("Missing color name"));
289
	return;
290
291
292
293
294
295
296
297
    }

    if (strstr(fgstr, ",")) {
	strtok(fgstr, ",");
	bgstr = strtok(NULL, ",");
    } else
	bgstr = NULL;

298
299
    fg = colortoint(fgstr, &bright);
    bg = colortoint(bgstr, &bright);
300

301
302
    if (syntaxes == NULL) {
	rcfile_error(_("Cannot add a color directive without a syntax line"));
303
	return;
304
305
306
307
308
309
    }

    for (tmpsyntax = syntaxes; tmpsyntax->next != NULL;
	 tmpsyntax = tmpsyntax->next)
	;

310
    /* Now the fun part, start adding regexps to individual strings
311
       in the colorstrings array, woo! */
312

313
    while (*ptr != '\0') {
314

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
	while (*ptr == ' ')
	    ptr++;

	if (*ptr == '\n' || *ptr == '\0')
	    break;

	if (!strncasecmp(ptr, "start=", 6)) {
	    ptr += 6;
	    expectend = 1;
	}

	if (*ptr != '"') {
	    rcfile_error(_("regex strings must begin and end with a \" character\n"));
	    continue;
	}
	ptr++;
331

332
333
334
335
336
337
	beginning = ptr;
	ptr = parse_next_regex(ptr);

	tmp = NULL;
	tmp = mallocstrcpy(tmp, beginning);

338
339
340
341
342
343
344
345
	if (tmpsyntax->color == NULL) {
	    tmpsyntax->color = nmalloc(sizeof(colortype));
	    tmpsyntax->color->fg = fg;
	    tmpsyntax->color->bg = bg;
	    tmpsyntax->color->bright = bright;
	    tmpsyntax->color->start = tmp;
	    tmpsyntax->color->next = NULL;
	    tmpcolor = tmpsyntax->color;
346
#ifdef DEBUG
347
348
349
350
	    fprintf(stderr,
		    "Starting a new colorstring for fg %d bg %d\n",
		    fg, bg);
	    fprintf(stderr, "string val=%s\n", tmp);
351
352
#endif

353
	} else {
354
	    for (tmpcolor = tmpsyntax->color;
355
		 tmpcolor->next != NULL; tmpcolor = tmpcolor->next);
356
#ifdef DEBUG
357
358
	    fprintf(stderr, "Adding new entry for fg %d bg %d\n", fg, bg);
	    fprintf(stderr, "string val=%s\n", tmp);
359
#endif
360

361
362
363
364
365
366
367
368
	    tmpcolor->next = nmalloc(sizeof(colortype));
	    tmpcolor->next->fg = fg;
	    tmpcolor->next->bg = bg;
	    tmpcolor->next->bright = bright;
	    tmpcolor->next->start = tmp;
	    tmpcolor->next->next = NULL;
	    tmpcolor = tmpcolor->next;
	}
369

370
371
372
373
374
375
	if (expectend) {
	    if (ptr == NULL || strncasecmp(ptr, "end=", 4)) {
		rcfile_error(_
			     ("\n\t\"start=\" requires a corresponding \"end=\""));
		return;
	    }
376

377
	    ptr += 4;
378

379
380
381
382
	    if (*ptr != '"') {
		rcfile_error(_
			     ("regex strings must begin and end with a \" character\n"));
		continue;
383
384
385
	    }
	    ptr++;

386

387
388
	    beginning = ptr;
	    ptr = parse_next_regex(ptr);
389
#ifdef DEBUG
390
391
	    fprintf(stderr, "For end part, beginning = \"%s\"\n",
		    beginning);
392
#endif
393
394
395
	    tmp = NULL;
	    tmp = mallocstrcpy(tmp, beginning);
	    tmpcolor->end = tmp;
396

397
398
	} else
	    tmpcolor->end = NULL;
399

400
    }
401
}
402
#endif				/* ENABLE_COLOR */
403

404
/* Parse the RC file, once it has been opened successfully */
405
void parse_rcfile(FILE * rcstream)
406
407
{
    char *buf, *ptr, *keyword, *option;
408
    int set = 0, i, j;
409

410
    buf = charalloc(1024);
411
412
413
    while (fgets(buf, 1023, rcstream) > 0) {
	lineno++;
	ptr = buf;
414
415
	while ((*ptr == ' ' || *ptr == '\t') &&
	       (*ptr != '\n' && *ptr != '\0'))
416
417
418
419
420
421
422
423
424
	    ptr++;

	if (*ptr == '\n' || *ptr == '\0')
	    continue;

	if (*ptr == '#') {
#ifdef DEBUG
	    fprintf(stderr, _("parse_rcfile: Read a comment\n"));
#endif
425
	    continue;		/* Skip past commented lines */
426
427
428
429
430
431
432
433
434
435
436
437
438
	}

	/* Else skip to the next space */
	keyword = ptr;
	ptr = parse_next_word(ptr);
	if (!ptr)
	    continue;

	/* Else try to parse the keyword */
	if (!strcasecmp(keyword, "set"))
	    set = 1;
	else if (!strcasecmp(keyword, "unset"))
	    set = -1;
439
#ifdef ENABLE_COLOR
440
441
	else if (!strcasecmp(keyword, "syntax"))
	    parse_syntax(rcstream, buf, ptr);
442
	else if (!strcasecmp(keyword, "color"))
443
	    parse_colors(rcstream, buf, ptr);
444
#endif				/* ENABLE_COLOR */
445
	else {
446
	    rcfile_msg(_("command %s not understood"), keyword);
447
448
449
450
451
452
453
454
	    continue;
	}

	option = ptr;
	ptr = parse_next_word(ptr);
	/* We don't care if ptr == NULL, as it should if using proper syntax */

	if (set != 0) {
455
	    for (i = 0; rcopts[i].name != ""; i++) {
456
		if (!strcasecmp(option, rcopts[i].name)) {
457
#ifdef DEBUG
458
459
		    fprintf(stderr, _("parse_rcfile: Parsing option %s\n"),
			    rcopts[i].name);
460
461
#endif
		    if (set == 1 || rcopts[i].flag == FOLLOW_SYMLINKS) {
462
			if (!strcasecmp(rcopts[i].name, "operatingdir") ||
463
			    !strcasecmp(rcopts[i].name, "tabsize") ||
464
#ifndef DISABLE_WRAPJUSTIFY
465
			    !strcasecmp(rcopts[i].name, "fill") ||
466
#endif
467
468
469
#ifndef DISABLE_JUSTIFY
			    !strcasecmp(rcopts[i].name, "quotestr") ||
#endif
470
471
472
#ifndef DISABLE_SPELLER
			    !strcasecmp(rcopts[i].name, "speller")
#else
473
			    0
474
#endif
475
			    ) {
476
477

			    if (*ptr == '\n' || *ptr == '\0') {
478
479
480
				rcfile_error(_
					     ("option %s requires an argument"),
					     rcopts[i].name);
481
482
483
484
485
				continue;
			    }
			    option = ptr;
			    ptr = parse_next_word(ptr);
			    if (!strcasecmp(rcopts[i].name, "fill")) {
486
487
#ifndef DISABLE_WRAPJUSTIFY

488
				if ((j = atoi(option)) < MIN_FILL_LENGTH) {
489
490
				    rcfile_error(_
						 ("requested fill size %d too small"),
491
						 j);
492
				} else
493
				    fill = j;
494
#endif
495
496
497
			    } else
				if (!strcasecmp(rcopts[i].name, "tabsize"))
			    {
498
				if ((j = atoi(option)) <= 0) {
499
500
				    rcfile_error(_
						 ("requested tab size %d too small"),
501
						 j);
502
				} else {
503
				    tabsize = j;
504
				}
505
506
507
508
509
510
511
512
513
#ifndef DISABLE_JUSTIFY
			    } else
				if (!strcasecmp(rcopts[i].name, "quotestr"))
			    {
				quotestr = NULL;
				quotestr =
				    charalloc(strlen(option) + 1);
				strcpy(quotestr, option);
#endif
514
			    } else {
515
#ifndef DISABLE_SPELLER
516
517
518
				alt_speller =
				    charalloc(strlen(option) + 1);
				strcpy(alt_speller, option);
519
#endif
520
			    }
521
			} else
522
523
			    SET(rcopts[i].flag);
#ifdef DEBUG
524
525
			fprintf(stderr, _("set flag %d!\n"),
				rcopts[i].flag);
526
527
528
529
#endif
		    } else {
			UNSET(rcopts[i].flag);
#ifdef DEBUG
530
531
			fprintf(stderr, _("unset flag %d!\n"),
				rcopts[i].flag);
532
#endif
533
		    }
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
		}
	    }
	}

    }
    if (errors)
	rcfile_error(_("Errors found in .nanorc file"));

    return;
}

/* The main rc file function, tries to open the rc file */
void do_rcfile(void)
{
    char *unable = _("Unable to open ~/.nanorc file, %s");
    struct stat fileinfo;
    FILE *rcstream;
Chris Allegretta's avatar
Chris Allegretta committed
551
    struct passwd *userage;
552

553
554
555
556
557
558
559
560
561
562
563
564
    nanorc = charalloc(strlen(SYSCONFDIR) + 10);
    sprintf(nanorc, "%s/nanorc", SYSCONFDIR);

    /* Try to open system nanorc */
    if (stat(nanorc, &fileinfo) != -1)
	if ((rcstream = fopen(nanorc, "r")) != NULL) {

	   /* Parse it! */
	    parse_rcfile(rcstream);
	    fclose(rcstream);
	}

Chris Allegretta's avatar
Chris Allegretta committed
565
    lineno = 0;
Chris Allegretta's avatar
Chris Allegretta committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579

    /* Determine home directory using getpwent(), don't rely on $HOME */
    for (userage = getpwent(); userage != NULL
	 && userage->pw_uid != geteuid(); userage = getpwent())
	;

    if (userage == NULL) {
	rcfile_error(_("I can't find my home directory!  Wah!"));
	return;
    }

    nanorc = charalloc(strlen(userage->pw_dir) + 10);
    sprintf(nanorc, "%s/.nanorc", userage->pw_dir);

580
581
582
583
584
    if (stat(nanorc, &fileinfo) == -1) {

	/* Abort if the file doesn't exist and there's some other kind
	   of error stat()ing it */
	if (errno != ENOENT)
Chris Allegretta's avatar
Chris Allegretta committed
585
	    rcfile_error(unable, strerror(errno));
586
	return;
587
    }
588
589
590
591
592
593

    if ((rcstream = fopen(nanorc, "r")) == NULL) {
	rcfile_error(unable, strerror(errno));
	return;
    }

594

595
    parse_rcfile(rcstream);
596
597
598
599
600
    fclose(rcstream);

}


601
#endif				/* ENABLE_NANORC */