rcfile.c 10.9 KB
Newer Older
1
2
/* $Id$ */
/**************************************************************************
3
 *   rcfile.c                                                             *
4
5
6
7
 *                                                                        *
 *   Copyright (C) 1999 Chris Allegretta                                  *
 *   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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 *   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>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "config.h"
#include "proto.h"
#include "nano.h"

#ifdef ENABLE_NANORC

#ifndef NANO_SMALL
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif

42
43
44
45
46
47
#ifndef DISABLE_WRAPJUSTIFY
    #define NUM_RCOPTS 18
#else
    #define NUM_RCOPTS 17
#endif

48
49
50
51
52
53
54
55
56
/* Static stuff for the nanorc file */
rcoption rcopts[NUM_RCOPTS] = 
{
{"regexp", USE_REGEXP},
{"const", CONSTUPDATE},
{"autoindent", AUTOINDENT},
{"cut", CUT_TO_END},
{"nofollow", FOLLOW_SYMLINKS},
{"mouse", USE_MOUSE},
57
{"operatingdir", 0},
58
{"pico", PICO_MODE},
59
{"tabsize", 0},
60
61

#ifndef DISABLE_WRAPJUSTIFY
62
{"fill", 0},
63
64
#endif

65
66
67
68
69
{"speller", 0},
{"tempfile", TEMP_OPT},
{"view", VIEW_MODE},
{"nowrap", NO_WRAP}, 
{"nohelp", NO_HELP}, 
70
{"suspend", SUSPEND},
71
72
{"multibuffer", MULTIBUFFER},
{"smooth", SMOOTHSCROLL}};
73

74
75
/* 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 */
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
void rcfile_error(char *msg, ...)
{
    va_list ap;

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

    while (getchar() != '\n')
	;

}

91
/* Just print the error (one of many, perhaps) but don't abort, yet */
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
void rcfile_msg(int *errors, char *msg, ...)
{
    va_list ap;

    if (!*errors) {
	*errors = 1;
	fprintf(stderr, "\n");
    }
    va_start(ap, msg);
    vfprintf(stderr, msg, ap);
    va_end(ap);
    fprintf(stderr, "\n");

}

107
/* Parse the next word from the string.  Returns NULL if we hit EOL */
108
109
char *parse_next_word(char *ptr)
{
110
    while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n' && ptr != '\0')
111
112
	ptr++;

113
    if (*ptr == '\0' || *ptr == '\n')
114
	return NULL;
115
	
116
117
118
    /* Null terminate and advance ptr */
    *ptr++ = 0;

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

122
123
124
    return ptr;
}

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
int colortoint(char *colorname, char *filename, int *lineno) 
{
    int mcolor = 0;

    if (colorname == NULL)
	return -1;

    if (strcasestr(colorname, "bright")) {
	mcolor += 8;
	colorname += 6;
    }
    
    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 {
	printf("Error in %s on line %d: color %s not understood.\n",
		filename, *lineno, colorname);
	printf("Valid colors are \"green\", \"red\", \"blue\", "
	       "\"white\", \"yellow\", \"cyan\", \"magenta\" and "
	       "\"black\", with the optional prefix \"bright\".\n");
	exit(1);
    }

    return mcolor;
}


#ifdef ENABLE_COLOR
/* Parse the color stuff into the colorstrings array */
void parse_colors(FILE *rcstream, char *filename, int *lineno, char *buf, char *ptr)
{
    int i = 0, fg, bg;
    char prev = '\\';
    char *tmp = NULL, *beginning, *fgstr, *bgstr;
    colortype *tmpcolor = NULL;
    colorstr *tmpstr = NULL;

    fgstr = ptr;
    ptr = parse_next_word(ptr);

    if (ptr == NULL) {
	printf("Error in %s on line %d: Missing color name.\n",
		filename, *lineno);
	exit(1);
    }

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

    fg = colortoint(fgstr, filename, lineno);
    bg = colortoint(bgstr, filename, lineno);

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

    i = 0;
    beginning = ptr;
    while (*ptr != '\0') {
	switch (*ptr) {
	case '\n':
	    *ptr = ' ';
Chris Allegretta's avatar
Chris Allegretta committed
203
/*	    i++; */
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
	case ' ':
	    if (prev != '\\') {
		/* This is the end of the regex, uh I guess.
		   Add it to the colorstrings array for this color */	
		
 		tmp = NULL;
		tmp = charalloc(i + 1);
		strncpy(tmp, beginning, i);
		tmp[i] = '\0';

		ptr = parse_next_word(ptr);
		if (ptr == NULL)
		    return;

		if (colorstrings == NULL) {
		    colorstrings = nmalloc(sizeof(colortype));
		    colorstrings->fg = fg;
		    colorstrings->bg = bg;
		    colorstrings->str = NULL;
		    colorstrings->str = nmalloc(sizeof(colorstr));
		    colorstrings->str->val = tmp;
		    colorstrings->str->next = NULL;
		    colorstrings->next = NULL;
		} else {
		    for (tmpcolor = colorstrings;
			tmpcolor->next != NULL && !(tmpcolor->fg == fg 
			&& tmpcolor->bg == bg); tmpcolor = tmpcolor->next)
			;

		    /* An entry for this color pair already exists, add it
			to the str list */
		    if (tmpcolor->fg == fg && tmpcolor->bg == bg) {
			for (tmpstr = tmpcolor->str; tmpstr->next != NULL;
				tmpstr = tmpstr->next)
			    ;

240
#ifdef DEBUG
241
		    fprintf(stderr, "Adding to existing entry for fg %d bg %d\n", fg, bg);
242
#endif
243
244
245
246
247
248

			tmpstr->next = nmalloc (sizeof(colorstr));
			tmpstr->next->val = tmp;
			tmpstr->next->next = NULL;
		    } else {

249
#ifdef DEBUG
250
		    fprintf(stderr, "Adding new entry for fg %d bg %d\n", fg, bg);
251
#endif
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

			tmpcolor->next = nmalloc(sizeof(colortype));
			tmpcolor->next->fg = fg;
			tmpcolor->next->bg = bg;
			tmpcolor->next->str = nmalloc(sizeof(colorstr));
			tmpcolor->next->str->val = tmp;
			tmpcolor->next->str->next = NULL;
			tmpcolor->next->next = NULL;
		    }
		}

		i = 0;
		beginning = ptr;
		break;
	    }
	    /* Else drop through to the default case */		
	default:
	    i++;
	    prev = *ptr;
	    ptr++;
	    break;
	}
    }

}
#endif /* ENABLE_COLOR */

279
280
281
282
283
284
285
/* Parse the RC file, once it has been opened successfully */
void parse_rcfile(FILE *rcstream, char *filename)
{
    char *buf, *ptr, *keyword, *option;
    int set = 0, lineno = 0, i;
    int errors = 0;

286
    buf = charalloc(1024);
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
    while (fgets(buf, 1023, rcstream) > 0) {
	lineno++;
	ptr = buf;
	while ((*ptr == ' ' || *ptr == '\t') && 
		(*ptr != '\n' && *ptr != '\0'))
	    ptr++;

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

	if (*ptr == '#') {
#ifdef DEBUG
	    fprintf(stderr, _("parse_rcfile: Read a comment\n"));
#endif
	    continue;	/* Skip past commented lines */
	}

	/* 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;
315
316
317
318
#ifdef ENABLE_COLOR
	else if (!strcasecmp(keyword, "color"))
	    parse_colors(rcstream, filename, &lineno, buf, ptr);
#endif /* ENABLE_COLOR */
319
320
321
322
323
324
325
326
327
328
329
330
331
332
	else {
	    rcfile_msg(&errors, _("Error in %s on line %d: command %s not understood"),
		filename, lineno, keyword);
	    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) {
	    for (i = 0; i <= NUM_RCOPTS - 1; i++) {
	        if (!strcasecmp(option, rcopts[i].name)) {
#ifdef DEBUG
333
		    fprintf(stderr, _("parse_rcfile: Parsing option %s\n"), 
334
335
336
				rcopts[i].name);
#endif
		    if (set == 1 || rcopts[i].flag == FOLLOW_SYMLINKS) {
337
			if (
338
			    !strcasecmp(rcopts[i].name, "operatingdir") ||
339
			    !strcasecmp(rcopts[i].name, "tabsize") ||
340
341
342
343
344
345
346
347
348
#ifndef DISABLE_WRAPJUSTIFY
			    !strcasecmp(rcopts[i].name, "fill") || 
#endif
#ifndef DISABLE_SPELLER
			    !strcasecmp(rcopts[i].name, "speller")
#else
				0
#endif
			   ) {
349
350
351
352
353
354
355
356
357

			    if (*ptr == '\n' || *ptr == '\0') {
	    			rcfile_msg(&errors, _("Error in %s on line %d: option %s requires an argument"),
						filename, lineno, rcopts[i].name);
				continue;
			    }
			    option = ptr;
			    ptr = parse_next_word(ptr);
			    if (!strcasecmp(rcopts[i].name, "fill")) {
358
359
#ifndef DISABLE_WRAPJUSTIFY

360
361
362
				if ((i = atoi(option)) < MIN_FILL_LENGTH) {
	    		 	    rcfile_msg(&errors, 
		_("Error in %s on line %d: requested fill size %d too small"),
363
						filename, lineno, i);
364
365
366
				}
				else
				     fill = i;
367
#endif
368
369
370
371
			    } else if (!strcasecmp(rcopts[i].name, "tabsize")) {
			    	if ((i = atoi(option)) <= 0) {
			    		rcfile_msg(&errors,
			    			_("Error in %s on line %d: requested tab size %d too small"),
372
			    				filename, lineno, i);
373
374
375
376
			    	} else {
			    		tabsize = i;
			    	}
			    } else {
377
#ifndef DISABLE_SPELLER
378
				alt_speller = charalloc(strlen(option) + 1);
379
            			strcpy(alt_speller, option);
380
#endif
381
382
383
384
			    }
			} else 
			    SET(rcopts[i].flag);
#ifdef DEBUG
385
			fprintf(stderr, _("set flag %d!\n"), rcopts[i].flag);
386
387
388
389
#endif
		    } else {
			UNSET(rcopts[i].flag);
#ifdef DEBUG
390
			fprintf(stderr, _("unset flag %d!\n"), rcopts[i].flag);
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#endif
		   }			
		}
	    }
	}

    }
    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 *nanorc;
    char *unable = _("Unable to open ~/.nanorc file, %s");
    struct stat fileinfo;
    FILE *rcstream;

412

413
414
415
    if (getenv("HOME") == NULL)
	return;

416
    nanorc = charalloc(strlen(getenv("HOME")) + 10);
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    sprintf(nanorc, "%s/.nanorc", getenv("HOME"));

    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)
	    rcfile_error(unable, errno);
	return;
   }

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

    parse_rcfile(rcstream, nanorc);
    fclose(rcstream);

}


#endif /* ENABLE_NANORC */