[go: up one dir, main page]

File: conffile.c

package info (click to toggle)
specter 1.3%2B1.4pre2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 728 kB
  • ctags: 531
  • sloc: ansic: 5,060; sh: 267; makefile: 238; perl: 169
file content (658 lines) | stat: -rw-r--r-- 15,243 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
203
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
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
275
276
277
278
279
280
281
282
283
284
285
286
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/* 
 * config file parser functions
 *
 * (C) 2000-2003 by Harald Welte <laforge@gnumonks.org>
 * (C) 2004 by Michal Kwiatkowski <ruby@joker.linuxstuff.pl>
 */

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 
 *  as published by the Free Software Foundation
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <conffile/conffile.h>


#ifdef DEBUG
#define DEBUGP(format, args...) do {				\
		fprintf(stderr, "%s:%i:", __FILE__, __LINE__);	\
		fprintf(stderr, format, ## args);		\
	} while (0)
#else
#define DEBUGP(format, args...)
#endif


#define SECTION_NAME_LEN 16
typedef struct section {
	struct section *	next;
	char			name[SECTION_NAME_LEN];
	long			begin;
	long			end;
	long			curr;
	short int		line;
	short int		line_curr;
	short int		flag;
} section_t;


#define BLOCK_NAME_LEN 32
typedef struct block {
	struct block *	next;
	char		name[BLOCK_NAME_LEN];
	short int	line,
			flag; /* set to 1 if block was accessed by config_block() */
	section_t *	sections; /* linked list of sections */
	section_t *	unrelated; /* pointer to unrelated options at the begging of block */
} block_t;


static FILE *fd;
static block_t *blocks;
static block_t *current_block;
static section_t *current_section;


/* 
 * get_word()  - parse a line into words
 * 
 * Arguments:     line   line to parse
 *                delim  possible word delimiters
 *                buf    pointer to buffer where word is returned
 * Return value:         pointer to first char after word
 * 
 * This function can deal with "" quotes.
 *    Written by Harald Welte, modified by Michal Kwiatkowski.
 */
static char *get_word(char *line, char *delim, char *buf)
{
	char *p, *start = NULL, *stop = NULL;
	int inquote = 0;

	for (p = line; *p; p++) {
		if (*p == '"') {
			start  = p + 1;
			inquote = 1;
			break;
		}
		if (!strchr(delim, *p)) {
			start = p;
			break;
		}
	}
	if (!start)
		return NULL;

	/* determine pointer to one char after word */
	for (p = start; *p; p++) {
		if (inquote) {
			if (*p == '"') {
				stop = p;
				break;
			}
		} else {
			if (strchr(delim, *p)) {
				stop = p;
				break;
			}
		}
	}
	if (!stop)
		return NULL;

	if (buf) {
		strncpy(buf, start, (size_t) (stop-start));
		*(buf + (stop-start)) = '\0';
	}

	/* skip quote character */
	if (inquote)
		/* yes, we can return stop + 1. If " was the last 
		 * character in string, it now points to NULL-term */
		return (stop + 1);

	return stop;
}


/*
 * read_line()  - reads one line from file and snips comments
 * 
 * Arguments:     line  buffer to store line
 * Return value:        number of lines read
 */
static int lines_read;
static int read_line(char *line, int len)
{
	char *p;

	if (fgets(line, len, fd) == NULL) {
		return 0;
	}
	lines_read++;

	/* comment should act as new line and string delimiter */
	if ((p = strchr(line, '#')) != NULL) {
		*p = '\n';
		*(p+1) = '\0';
	}

	/* look for line continuation: escape and a newline */
	if ((p = strstr(line, "\\\n")) != NULL) {
		/* recursion - the easiest way */
		*p = '\0';
		len -= strlen(line);
		if (len <= 0)
			return 0;
		if (read_line(p, len) == 0)
			return 0;
	}

	return lines_read;
}


/*
 * find_key()  - looks for config entry with given key name in list
 * 
 * Arguments:     name  name of option we're looking for
 *                list  list of config entries to check
 * Return value:        pointer to config entry or NULL if not found
 */
static config_entry_t *find_key(char *name, config_entry_t *list)
{
	config_entry_t *ce;

	for (ce = list; ce; ce = ce->next) {
		if (!strncmp(name, ce->key, CONFIG_KEY_LEN))
			return ce;
	}

	return NULL;
}


/*
 * mtoi()  - changes memory string into plain integer
 *
 * Arguments:     str   string to parse
 * Return value:        integer value of given string
 */
static int mtoi(char *str)
{
	char number[32];
	char *last;
	int factor = 1;

	strncpy(number, str, 31);
	last = &(number[strlen(number) - 1]);
	switch (*last) {
		case 'M':
			factor *= 1024;
		case 'K':
			factor *= 1024;
			*last = '\0';
		default:
			break;
	}

	return (atoi(number) * factor);
}


/* 
 * config_open()  - opens a file and divide it into blocks
 * 
 * Arguments:     path  points to config file
 * Return value:        number of blocks found
 *
 * Block starts with string and curly bracket '{', while ends with '}'.
 * No nesting is allowed.
 * Section names begin with a colon, and can be only defined inside blocks.
 * '#' is treated as a comment.
 */
int config_open(char *path)
{
	int found = 0, line_ctr = 0, inside_block = 0, inside_section = 0;
	block_t *bl = NULL;
	section_t *se = NULL;

	if ((fd = fopen(path, "r")) == NULL) {
		fprintf(stderr, "Couldn't open config file %s: %s\n",
				path, strerror(errno));
		return 0;
	}

	/* search for blocks */
	do {
		int ret;
		char line[CONFIG_LINE_LEN];
		char id[CONFIG_VALUE_LEN], bracket[CONFIG_VALUE_LEN];
		char *p;

		/* block can end at the same line it started,
		 * so let's check for this first */
		if (inside_block) {
			char *s;
			if ((s = strchr(line, '}')) != NULL) {
				if (get_word(s+1, " \t\n", NULL)) {
					fprintf(stderr, "Config error at line %d: "
						"trailing characters after end of block.\n",
						line_ctr);
					return 0;
				}
				/* end of block means end of last section */
				if (inside_section) {
					se->end = ftell(fd) - strlen(line) + (s - line);
				}
				found++;
				inside_block = inside_section = 0;
			}
			if (strchr(line, '{')) {
				fprintf(stderr, "Config error at line %d: "
						"nesting of blocks forbidden.\n",
						line_ctr);
				return 0;
			}
		}

		lines_read = 0;
		if ((ret = read_line(line, CONFIG_LINE_LEN-1)) == 0)
			break;
		line_ctr += ret;

		/* if we're inside block, just check sections beginings,
		 * validity will be checked during config_read() */
		if (inside_block) {
			p = line;
			if ((p = get_word(p, " \t\n", id)) == NULL)
				continue;
			if (id[0] == ':') {
				/* end previous section first */
				if (inside_section) {
					se->end = ftell(fd) - strlen(line);
				}
				if ((se = (section_t *)malloc(sizeof(section_t))) == NULL) {
					fprintf(stderr, "config_open(): out of memory.\n");
					return 0;
				}
				if (id[1] == '\0') {
					if ((p = get_word(p, " \t\n", id)) == NULL) {
						fprintf(stderr, "Config error at line %d: "
							"section name missing.\n",
							line_ctr);
						return 0;
					}
					strncpy(se->name, id, SECTION_NAME_LEN);
				} else {
					strncpy(se->name, &id[1], SECTION_NAME_LEN);
				}
				se->next = NULL;
				se->curr = se->begin = ftell(fd);
				se->line_curr = se->line = line_ctr+1;
				se->flag = 0;
				if (bl->sections == NULL) {
					bl->sections = se;
				} else {
					section_t *last = bl->sections;
					while (last->next)
						last = last->next;
					last->next = se;
				}
				inside_section = 1;
			} else if (!inside_section) {
				if ((se = (section_t *)malloc(sizeof(section_t))) == NULL) {
					fprintf(stderr, "config_open(): out of memory.\n");
					return 0;
				}
				se->next = NULL;
				se->name[0] = '\0';
				se->curr = se->begin = ftell(fd) - strlen(line);
				se->line_curr = se->line = line_ctr;
				se->flag = 0;
				bl->unrelated = se;
				inside_section = 1;
			}
			continue;
		}

		/* read two words, second should be curly bracket */
		p  = line;
		if ((p = get_word(p, " \t\n", id)) == NULL)
			continue;
		p = get_word(p, " \t\n", bracket);
		if (p == NULL || bracket[0] != '{') {
			fprintf(stderr, "Config error at line %d: "
					"\"%s\" is not a block.\n",
					line_ctr, id);
			return 0;
		}

		for (bl = blocks; bl; bl = bl->next) {
			if (!strcmp(bl->name, id)) {
				fprintf(stderr, "Config error at line %d: "
						"redefinition of block \"%s\".\n",
						line_ctr, id);
				return 0;
			}
		}

		/* create new block_t structure and fill it */
		if ((bl = (block_t *)malloc(sizeof(block_t))) == NULL) {
			fprintf(stderr, "config_open(): out of memory.\n");
			return 0;
		}
		if (!blocks) {
			blocks = bl;
		} else {
			block_t *tmp = blocks;
			while (tmp->next)
				tmp = tmp->next;
			tmp->next = bl;
		}
		bl->next = NULL;
		strncpy(bl->name, id, BLOCK_NAME_LEN);
		bl->line = line_ctr;
		bl->flag = 0;
		inside_block = 1;
		/* clear start of block, so it won't drop into
		 * condition at line 263 */
		*(strchr(line, '{')) = ' ';

	} while (!feof(fd));

	if (inside_block) {
		fprintf(stderr, "Config error at line %d: "
				"unexpected end of file.\n", line_ctr);
		return 0;
	}

	return found;
}


/*
 * set_block_section()  - set given block and section combination as current
 *
 * Arguments:     block     block name
 *                section   section name (or NULL if you want unrelated options)
 *                rewind    if 1 current position will be reset to section's begining
 * Return value:            0 if not found and starting line otherwise
 */
static int set_block_section(const char *block, const char *section, const int rewind)
{
	block_t *p;
	section_t *s;

	for (p = blocks; p; p = p->next) {
		if (!strcmp(p->name, block)) {
			if (section == NULL) {
				if ((s = p->unrelated) == NULL)
					return 0;
				goto found;
			}
			for (s = p->sections; s; s = s->next) {
				if (!strcmp(s->name, section))
					goto found;
			}
		}
	}

	/* not found */
	return 0;

found:
	current_block = p;
	current_section =  s;
	if (rewind) {
		s->curr = s->begin;
		s->line_curr = s->line;
	}
	fseek(fd, s->curr, SEEK_SET);
	p->flag = s->flag = 1;
	return s->line_curr;
}


/* 
 * is_section()  - checks if section in given block exists
 *
 * Arguments:     block     block name
 *                section   section name or NULL for unrelated
 * Return value:            1 if found, 0 if not
 */
int is_section(const char *block, const char *section)
{
	block_t *p;
	section_t *s;

	for (p = blocks; p; p = p->next) {
		if (!strcmp(p->name, block)) {
			if (section == NULL) {
				if (p->unrelated != NULL)
					return 1;
				else
					continue;
			}
			for (s = p->sections; s; s = s->next) {
				if (!strcmp(s->name, section)) {
					return 1;
				}
			}
		}
	}

	return 0;
}


/*
 * is_block()  - checks if given block exists
 *
 * Arguments:     block     block name
 * Return value:            1 if found, 0 if not
 */
int is_block(const char *block)
{
	block_t *p;

	for (p = blocks; p; p = p->next) {
		if (!strcmp(p->name, block)) {
			return 1;
		}
	}

	return 0;
}


/*
 * config_read()  - read options from current block
 *
 * Arguments:     block    block to read options from
 *                section  section to read options from
 *                list     list of config entries to fill
 *                flags    see conffile.h for CONFIG_READ_*
 * Return value:           see conffile.h for CONFIG_RET_*
 *
 * Function normally returns after parsing all options or after reading
 * entry with CONFIG_OPT_MULTI/CONFIG_OPT_ANY set. In the second case
 * function must be invoked many times to read all these options.
 */
int config_read(const char *block, const char *section, config_entry_t *list, const int flags)
{
	int current_line;
	config_entry_t *ce;
	char line[CONFIG_LINE_LEN];
	char wordbuf[CONFIG_VALUE_LEN];
	char *wordend, *p;

	if ((current_line = set_block_section(block, section, flags & CONFIG_READ_REWIND)) == 0)
		return CONFIG_RET_MISSING;

	while (ftell(fd) < current_section->end) {
		int ret;

		lines_read = 0;
		if ((ret = read_line(line, CONFIG_LINE_LEN-1)) == 0)
			break;
		current_line += ret;

		/* terminate at end of block */
		if ((p = strchr(line, '}')) != NULL) {
			*p = '\n';
			*(p+1) = '\0';
		}

		if ((wordend = get_word(line, " \t\n", wordbuf)) == NULL)
			continue;

		if (list->options == CONFIG_OPT_ANY) {
			ce = list;
			strncpy(list->key, wordbuf, sizeof(list->key));
		} else if ((ce = find_key(wordbuf, list)) == NULL) {
			if ((flags & CONFIG_READ_IGNORE)) {
				continue;
			} else {
				fprintf(stderr, "Config error at line %d: "
						"bad option \"%s\".\n",
						current_line, wordbuf);
				return CONFIG_RET_ERROR;
			}
		}

		if (ce->options == CONFIG_OPT_IGNORE)
			continue;

		if (ce->hit) {
			fprintf(stderr, "Config error at line %d: "
					"redefinition of option \"%s\".\n",
					current_line, wordbuf);
			return CONFIG_RET_ERROR;
		}

		if (ce->type != CONFIG_TYPE_BOOLEAN) {
			if ((wordend = get_word(wordend, " \t\n", wordbuf)) == NULL) {
				fprintf(stderr, "Config error at line %d: "
					"no value for option \"%s\".\n",
					current_line, wordbuf);
				return CONFIG_RET_ERROR;
			}
		}

		if (get_word(wordend, " \t\n", wordbuf) != NULL) {
			fprintf(stderr, "Config error at line %d: "
					"trailing characters after option.\n",
					current_line);
			return CONFIG_RET_ERROR;
		}

		switch (ce->type) {
			case CONFIG_TYPE_BOOLEAN:
				ce->u.value = 1;
				break;
			case CONFIG_TYPE_INT:
				ce->u.value = strtol(wordbuf, NULL, 10);
				break;
			case CONFIG_TYPE_HEX:
				ce->u.value = strtol(wordbuf, NULL, 16);
				break;
			case CONFIG_TYPE_STRING:
				strncpy(ce->u.string, wordbuf, CONFIG_VALUE_LEN);
				break;
			case CONFIG_TYPE_MEM:
				ce->u.value = mtoi(wordbuf);
				break;
			default:
				DEBUGP("config_read(): bad config type %i.\n", ce->type);
				break;
		}

		if (ce->options == CONFIG_OPT_MULTI || ce->options == CONFIG_OPT_ANY) {
			current_section->line_curr = current_line;
			current_section->curr = ftell(fd);
			return CONFIG_RET_MORE;
		} else {
			ce->hit = 1;
		}
	}

	for (ce = list; ce; ce = ce->next) {
		if ((ce->options == CONFIG_OPT_MANDATORY) && !ce->hit) {
			fprintf(stderr, "Config error in %s::%s: "
					"mandatory option \"%s\" not set.\n",
					current_block->name, current_section->name,
					ce->key);
			return CONFIG_RET_ERROR;
		}
	}

	current_section->line_curr = current_line;
	current_section->curr = ftell(fd);

	return CONFIG_RET_OK;
}


/*
 * config_close()  - closes config file and frees all block descriptors
 *
 * Arguments:     none
 * Return value:        -1 on error
 */
int config_close(void)
{
	block_t *bl;
	section_t *se;

	if (fclose(fd) == EOF) {
		DEBUGP("config_close(): Couldn't close config file.\n");
		return -1;
	}

	for (bl = blocks; bl; bl = bl->next) {
		if (!bl->flag) {
			fprintf(stderr, "Config warning at line %d: "
					"block \"%s\" unused.\n",
					bl->line, bl->name);
		}
	}

	current_block = blocks;
	while (current_block) {
		bl = current_block->next;

		current_section = current_block->sections;
		while (current_section) {
			se = current_section->next;
			free(current_section);
			current_section = se;
		}
		if (current_block->unrelated) {
			free(current_block->unrelated);
		}

		free(current_block);
		current_block = bl;
	}
	blocks = NULL;
	/* current_block and current_section end up being NULL */

	return 0;
}