configparser.c 38.2 KB
Newer Older
Bernhard Link's avatar
Bernhard Link committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*  This file is part of "reprepro"
 *  Copyright (C) 2007 Bernhard R. Link
 *  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., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301  USA
 */
#include <config.h>

#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
25
#include <dirent.h>
Bernhard Link's avatar
Bernhard Link committed
26 27 28

#include "error.h"
#include "names.h"
29
#include "atoms.h"
30
#include "filecntl.h"
Bernhard Link's avatar
Bernhard Link committed
31 32 33 34 35
#include "configparser.h"

struct configiterator {
	FILE *f;
	unsigned int startline, line, column, markerline, markercolumn;
36
	const char *filename;
37
	const char *chunkname;
38
	bool eol;
Bernhard Link's avatar
Bernhard Link committed
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
};

const char *config_filename(const struct configiterator *iter) {
	return iter->filename;
}

unsigned int config_line(const struct configiterator *iter) {
	return iter->line;
}

unsigned int config_column(const struct configiterator *iter) {
	return iter->column;
}

unsigned int config_firstline(const struct configiterator *iter) {
	return iter->startline;
}

unsigned int config_markerline(const struct configiterator *iter) {
	return iter->markerline;
}

unsigned int config_markercolumn(const struct configiterator *iter) {
	return iter->markercolumn;
}

void config_overline(struct configiterator *iter) {
	int c;

68
	while (!iter->eol) {
Bernhard Link's avatar
Bernhard Link committed
69
		c = fgetc(iter->f);
70
		if (c == '#') {
Bernhard Link's avatar
Bernhard Link committed
71 72
			do {
				c = fgetc(iter->f);
73
			} while (c != EOF && c != '\n');
Bernhard Link's avatar
Bernhard Link committed
74
		}
75
		if (c == EOF || c == '\n')
76
			iter->eol = true;
Bernhard Link's avatar
Bernhard Link committed
77 78 79 80 81
		else
			iter->column++;
	}
}

82
bool config_nextline(struct configiterator *iter) {
Bernhard Link's avatar
Bernhard Link committed
83 84
	int c;

85
	assert (iter->eol);
Bernhard Link's avatar
Bernhard Link committed
86
	c = fgetc(iter->f);
87
	while (c == '#') {
Bernhard Link's avatar
Bernhard Link committed
88 89
		do {
			c = fgetc(iter->f);
90
		} while (c != EOF && c != '\n');
Bernhard Link's avatar
Bernhard Link committed
91 92 93
		iter->line++;
		c = fgetc(iter->f);
	}
94
	if (c == EOF)
Bernhard Link's avatar
Bernhard Link committed
95
		return false;
96
	if (c == ' ' || c == '\t') {
Bernhard Link's avatar
Bernhard Link committed
97 98
		iter->line++;
		iter->column = 1;
99 100
		iter->eol = false;
		return true;
Bernhard Link's avatar
Bernhard Link committed
101
	}
102
	(void)ungetc(c, iter->f);
103
	return false;
Bernhard Link's avatar
Bernhard Link committed
104 105
}

106
retvalue linkedlistfinish(UNUSED(void *privdata), void *this, void **last, UNUSED(bool complete), UNUSED(struct configiterator *dummy3)) {
Bernhard Link's avatar
Bernhard Link committed
107 108 109 110
	*last = this;
	return RET_NOTHING;
}

111
static inline retvalue finishchunk(configfinishfunction finishfunc, void *privdata, struct configiterator *iter, const struct configfield *fields, size_t fieldcount, bool *found, void **this, void **last, bool complete) {
Bernhard Link's avatar
Bernhard Link committed
112 113 114
	size_t i;
	retvalue r;

115 116 117
	if (complete)
		for (i = 0 ; i < fieldcount ; i++) {
			if (!fields[i].required)
Bernhard Link's avatar
Bernhard Link committed
118
				continue;
119
			if (found[i])
Bernhard Link's avatar
Bernhard Link committed
120 121 122
				continue;
			fprintf(stderr,
"Error parsing config file %s, line %u:\n"
123 124
"Required field '%s' not found in\n"
"%s starting in line %u and ending in line %u.\n",
Bernhard Link's avatar
Bernhard Link committed
125
					iter->filename, iter->line,
126 127
					fields[i].name, iter->chunkname,
					iter->startline, iter->line-1);
128
			(void)finishfunc(privdata, *this, last, false, iter);
Bernhard Link's avatar
Bernhard Link committed
129 130 131 132 133 134 135 136
			*this = NULL;
			return RET_ERROR_MISSING;
		}
	r = finishfunc(privdata, *this, last, complete, iter);
	*this = NULL;
	return r;
}

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
char *configfile_expandname(const char *filename, char *fndup) {
	const char *fromdir;
	char *n;

	assert (fndup == NULL || fndup == filename);

	if (filename[0] == '/' || (filename[0] == '.' && filename[1] == '/'))
		return fndup?fndup:strdup(filename);
	if (filename[0] == '~' && filename[1] == '/') {
		n = calc_dirconcat(getenv("HOME"), filename + 2);
		free(fndup);
		return n;
	}
	if (filename[0] != '+' || filename[1] == '\0' || filename[2] != '/') {
		n = calc_dirconcat(global.confdir, filename);
		free(fndup);
		return n;
	}
	if (filename[1] == 'b') {
		fromdir = global.basedir;
	} else if (filename[1] == 'o') {
		fromdir = global.outdir;
	} else if (filename[1] == 'c') {
		fromdir = global.confdir;
	} else {
		fprintf(stderr, "Warning: strange filename '%s'!\n",
				filename);
		return fndup?fndup:strdup(filename);
	}
	n = calc_dirconcat(fromdir, filename + 3);
	free(fndup);
	return n;
}

171 172
static retvalue configfile_parse_multi(/*@only@*/char *, bool, configinitfunction, configfinishfunction, const char *, const struct configfield *, size_t, void *, int, void **, struct strlist *);

173
static retvalue configfile_parse_single(/*@only@*/char *filename, bool ignoreunknown, configinitfunction initfunc, configfinishfunction finishfunc, const char *chunkname, const struct configfield *fields, size_t fieldcount, void *privdata, int depth, void **last_p, struct strlist *filenames) {
174
	bool found[fieldcount];
175
	void *this = NULL;
Bernhard Link's avatar
Bernhard Link committed
176 177 178 179 180 181
	char key[100];
	size_t keylen;
	int c, ret;
	size_t i;
	struct configiterator iter;
	retvalue result, r;
182
	bool afterinclude = false;
Bernhard Link's avatar
Bernhard Link committed
183

184
	if (strlist_in(filenames, filename)) {
185 186
		if (verbose >= 0) {
			fprintf(stderr,
187
"Ignoring subsequent inclusion of '%s'!\n", filename);
188
		}
189
		free(filename);
190 191
		return RET_NOTHING;
	}
192 193
	iter.filename = filename;
	r = strlist_add(filenames, filename);
194 195
	if (RET_WAS_ERROR(r))
		return r;
196
	iter.chunkname = chunkname;
Bernhard Link's avatar
Bernhard Link committed
197 198 199 200
	iter.line = 0;
	iter.column = 0;

	iter.f = fopen(iter.filename, "r");
201
	if (iter.f == NULL) {
Bernhard Link's avatar
Bernhard Link committed
202 203 204 205 206 207 208 209 210 211 212
		int e = errno;
		fprintf(stderr, "Error opening config file '%s': %s(%d)\n",
				iter.filename, strerror(e), e);
		return RET_ERRNO(e);
	}
	result = RET_NOTHING;
	do {
		iter.line++;
		iter.column = 1;

		c = fgetc(iter.f);
213
		while (c == '#') {
Bernhard Link's avatar
Bernhard Link committed
214 215
			do {
				c = fgetc(iter.f);
216
			} while (c != EOF && c != '\n');
Bernhard Link's avatar
Bernhard Link committed
217 218 219
			iter.line++;
			c = fgetc(iter.f);
		}
220
		if (c == '\r')  {
Bernhard Link's avatar
Bernhard Link committed
221 222
			do {
				c = fgetc(iter.f);
223 224 225 226
			} while (c == '\r');
			if (c != EOF && c != '\n') {
				fprintf(stderr,
"%s:%u: error parsing configuration file: CR without following LF!\n",
Bernhard Link's avatar
Bernhard Link committed
227 228 229 230 231
						iter.filename, iter.line);
				result = RET_ERROR;
				break;
			}
		}
232
		if (c == EOF)
Bernhard Link's avatar
Bernhard Link committed
233
			break;
234
		if (c == '\n') {
235
			afterinclude = false;
Bernhard Link's avatar
Bernhard Link committed
236
			/* Ignore multiple emptye lines */
237
			if (this == NULL)
Bernhard Link's avatar
Bernhard Link committed
238 239 240 241
				continue;
			/* finish this chunk, to get ready for the next: */
			r = finishchunk(finishfunc, privdata, &iter,
					fields, fieldcount, found,
242
					&this, last_p, true);
243
			if (RET_WAS_ERROR(r)) {
Bernhard Link's avatar
Bernhard Link committed
244 245 246 247 248
				result = r;
				break;
			}
			continue;
		}
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
		if (afterinclude) {
			fprintf(stderr,
"Warning parsing %s, line %u: no empty line after '!include'-sequence"
" might cause ambiguity in the future!\n",
					iter.filename, iter.line);
			afterinclude = false;
		}
		if (c == '!') {
			keylen = 0;
			while ((c = fgetc(iter.f)) != EOF && c >= 'a' && c <= 'z') {
				iter.column++;
				key[keylen++] = c;
				if (keylen >= 10)
					break;
			}
			if (c != ':') {
				fprintf(stderr,
"Error parsing %s, line %u: invalid !-sequence!\n",
					iter.filename, iter.line);
				result = RET_ERROR;
				break;
			}
			iter.column++;
			if (keylen == 7 && memcmp(key, "include", 7) == 0) {
				char *filetoinclude;

				if (this != NULL) {
					fprintf(stderr,
"Error parsing %s, line %u: '!include' statement within unterminated %s!\n"
"(perhaps you forgot to put an empty line before this)\n",
						iter.filename, iter.line,
						chunkname);
					result = RET_ERROR;
					break;
				}
				if (depth > 20) {
					fprintf(stderr,
"Error parsing %s, line %u: too many nested '!include' statements!\n",
						iter.filename, iter.line);
					result = RET_ERROR;
					break;
				}
				r = config_getonlyword(&iter, "!include",
						NULL, &filetoinclude);
				if (RET_WAS_ERROR(r)) {
					result = r;
					break;
				}
297 298
				filetoinclude = configfile_expandname(
						filetoinclude, filetoinclude);
299
				r = configfile_parse_multi(filetoinclude,
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
						ignoreunknown,
						initfunc, finishfunc,
						chunkname,
						fields, fieldcount,
						privdata, depth + 1,
						last_p, filenames);
				if (RET_WAS_ERROR(r)) {
					result = r;
					break;
				}
				afterinclude = true;
			} else {
				key[keylen] = '\0';
				fprintf(stderr,
"Error parsing %s, line %u: unknown !-sequence '%s'!\n",
					iter.filename, iter.line, key);
				result = RET_ERROR;
				break;
			}
			/* ignore all data left of this field */
			do {
				config_overline(&iter);
			} while (config_nextline(&iter));
			continue;
		}
325 326 327
		if (c == '\0') {
			fprintf(stderr,
"Error parsing %s, line %u: \\000 character not allowed in config files!\n",
Bernhard Link's avatar
Bernhard Link committed
328 329 330 331
					iter.filename, iter.line);
			result = RET_ERROR;
			break;
		}
332
		if (c == ' ' || c == '\t') {
333 334
			fprintf(stderr,
"Error parsing %s, line %u: unexpected white space before keyword!\n",
Bernhard Link's avatar
Bernhard Link committed
335 336 337 338 339 340 341
					iter.filename, iter.line);
			result = RET_ERROR;
			break;
		}
		key[0] = c;
		keylen = 1;

342 343
		while ((c = fgetc(iter.f)) != EOF && c != ':' && c != '\n'
				&& c != '#' && c != '\0') {
Bernhard Link's avatar
Bernhard Link committed
344
			iter.column++;
345 346 347
			if (c == ' ') {
				fprintf(stderr,
"Error parsing %s, line %u: Unexpected space in header name!\n",
Bernhard Link's avatar
Bernhard Link committed
348
						iter.filename, iter.line);
349 350
				result = RET_ERROR;
				break;
Bernhard Link's avatar
Bernhard Link committed
351
			}
352 353 354
			if (c == '\t') {
				fprintf(stderr,
"Error parsing %s, line %u: Unexpected tabulator character in header name!\n",
355 356 357 358 359
						iter.filename, iter.line);
				result = RET_ERROR;
				break;
			}
			key[keylen++] = c;
360
			if (keylen >= 100)
361
				break;
Bernhard Link's avatar
Bernhard Link committed
362
		}
363 364
		if (c != ':') {
			if (c != ' ' && c != '\t')
365
				/* newline or end-of-file */
366 367
				fprintf(stderr,
"Error parsing %s, line %u, column %u: Colon expected!\n",
368
					iter.filename, iter.line, iter.column);
Bernhard Link's avatar
Bernhard Link committed
369 370 371
			result = RET_ERROR;
			break;
		}
372
		if (this == NULL) {
Bernhard Link's avatar
Bernhard Link committed
373
			/* new chunk, initialize everything */
374
			r = initfunc(privdata, *last_p, &this);
375 376
			assert (r != RET_NOTHING);
			if (RET_WAS_ERROR(r)) {
Bernhard Link's avatar
Bernhard Link committed
377 378 379
				result = r;
				break;
			}
380
			assert (this != NULL);
Bernhard Link's avatar
Bernhard Link committed
381 382 383
			iter.startline = iter.line;
			memset(found, 0, sizeof(found));
		}
384 385
		for (i = 0 ; i < fieldcount ; i++) {
			if (keylen != fields[i].namelen)
Bernhard Link's avatar
Bernhard Link committed
386
				continue;
387
			if (strncasecmp(key, fields[i].name, keylen) != 0)
Bernhard Link's avatar
Bernhard Link committed
388 389 390
				continue;
			break;
		}
391
		if (i >= fieldcount) {
Bernhard Link's avatar
Bernhard Link committed
392
			key[keylen] = '\0';
393 394 395
			if (!ignoreunknown) {
				fprintf(stderr,
"Error parsing %s, line %u: Unknown header '%s'!\n",
Bernhard Link's avatar
Bernhard Link committed
396 397 398 399
						iter.filename, iter.line, key);
				result = RET_ERROR_UNKNOWNFIELD;
				break;
			}
400 401 402
			if (verbose >= 0)
				fprintf(stderr,
"Warning parsing %s, line %u: Unknown header '%s'!\n",
Bernhard Link's avatar
Bernhard Link committed
403
						iter.filename, iter.line, key);
404 405 406 407
		} else if (found[i]) {
			fprintf(stderr,
"Error parsing %s, line %u: Second appearance of '%s' in the same chunk!\n",
				iter.filename, iter.line, fields[i].name);
Bernhard Link's avatar
Bernhard Link committed
408 409 410
			result = RET_ERROR;
			break;
		} else
411
			found[i] = true;
Bernhard Link's avatar
Bernhard Link committed
412 413 414
		do {
			c = fgetc(iter.f);
			iter.column++;
415
		} while (c == ' ' || c == '\t');
416
		(void)ungetc(c, iter.f);
Bernhard Link's avatar
Bernhard Link committed
417

418
		iter.eol = false;
419
		if (i < fieldcount) {
420
			r = fields[i].setfunc(privdata, fields[i].name, this, &iter);
Bernhard Link's avatar
Bernhard Link committed
421
			RET_UPDATE(result, r);
422
			if (RET_WAS_ERROR(r))
Bernhard Link's avatar
Bernhard Link committed
423 424 425 426 427
				break;
		}
		/* ignore all data left of this field */
		do {
			config_overline(&iter);
428 429 430
		} while (config_nextline(&iter));
	} while (true);
	if (this != NULL) {
Bernhard Link's avatar
Bernhard Link committed
431 432
		r = finishchunk(finishfunc, privdata, &iter,
				fields, fieldcount, found,
433
				&this, last_p,
Bernhard Link's avatar
Bernhard Link committed
434 435 436
				!RET_WAS_ERROR(result));
		RET_UPDATE(result, r);
	}
437
	if (ferror(iter.f) != 0) {
Bernhard Link's avatar
Bernhard Link committed
438 439 440 441 442 443 444
		int e = errno;
		fprintf(stderr, "Error reading config file '%s': %s(%d)\n",
				iter.filename, strerror(e), e);
		r = RET_ERRNO(e);
		RET_UPDATE(result, r);
	}
	ret = fclose(iter.f);
445
	if (ret != 0) {
Bernhard Link's avatar
Bernhard Link committed
446 447 448 449 450 451 452 453 454
		int e = errno;
		fprintf(stderr, "Error closing config file '%s': %s(%d)\n",
				iter.filename, strerror(e), e);
		r = RET_ERRNO(e);
		RET_UPDATE(result, r);
	}
	return result;
}

455
static retvalue configfile_parse_multi(/*@only@*/char *fullfilename, bool ignoreunknown, configinitfunction initfunc, configfinishfunction finishfunc, const char *chunkname, const struct configfield *fields, size_t fieldcount, void *privdata, int depth, void **last_p, struct strlist *filenames) {
456
	retvalue result = RET_NOTHING, r;
457

458 459 460 461
	if (isdirectory(fullfilename)) {
		DIR *dir;
		struct dirent *de;
		int e;
462
		char *subfilename;
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

		dir = opendir(fullfilename);
		if (dir == NULL) {
			e = errno;
			fprintf(stderr,
"Error %d opening directory '%s': %s\n",
				e, fullfilename, strerror(e));
			free(fullfilename);
			return RET_ERRNO(e);
		}
		while ((errno = 0, de = readdir(dir)) != NULL) {
			size_t l;
			if (de->d_type != DT_REG && de->d_type != DT_LNK
					&& de->d_type != DT_UNKNOWN)
				continue;
			if (de->d_name[0] == '.')
				continue;
			l = strlen(de->d_name);
			if (l < 5 || strcmp(de->d_name + l - 5, ".conf") != 0)
				continue;
			subfilename = calc_dirconcat(fullfilename, de->d_name);
			if (FAILEDTOALLOC(subfilename)) {
				(void)closedir(dir);
				free(fullfilename);
				return RET_ERROR_OOM;
			}
			r = configfile_parse_single(subfilename, ignoreunknown,
				initfunc, finishfunc,
				chunkname, fields, fieldcount, privdata,
492
				depth, last_p, filenames);
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
			RET_UPDATE(result, r);
			if (RET_WAS_ERROR(r)) {
				(void)closedir(dir);
				free(fullfilename);
				return r;
			}
		}
		e = errno;
		if (e != 0) {
			(void)closedir(dir);
			fprintf(stderr,
"Error %d reading directory '%s': %s\n",
				e, fullfilename, strerror(e));
			free(fullfilename);
			return RET_ERRNO(e);
		}
		if (closedir(dir) != 0) {
			e = errno;
			fprintf(stderr,
"Error %d closing directory '%s': %s\n",
				e, fullfilename, strerror(e));
			free(fullfilename);
			return RET_ERRNO(e);
		}
		free(fullfilename);
	} else {
		r = configfile_parse_single(fullfilename, ignoreunknown,
				initfunc, finishfunc,
				chunkname, fields, fieldcount, privdata,
522
				depth, last_p, filenames);
523 524
		RET_UPDATE(result, r);
	}
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	return result;
}

retvalue configfile_parse(const char *filename, bool ignoreunknown, configinitfunction initfunc, configfinishfunction finishfunc, const char *chunkname, const struct configfield *fields, size_t fieldcount, void *privdata) {
	struct strlist filenames;
	void *last = NULL;
	retvalue r;
	char *fullfilename;

	fullfilename = configfile_expandname(filename, NULL);
	if (fullfilename == NULL)
		return RET_ERROR_OOM;

	strlist_init(&filenames);

	r = configfile_parse_multi(fullfilename, ignoreunknown,
			initfunc, finishfunc,
			chunkname, fields, fieldcount, privdata,
			0, &last, &filenames);

545 546
	/* only free filenames last, as they might still be
	 * referenced while running */
547
	strlist_done(&filenames);
548
	return r;
549 550
}

Bernhard Link's avatar
Bernhard Link committed
551 552 553 554 555 556
static inline int config_nextchar(struct configiterator *iter) {
	int c;
	unsigned int realcolumn;

	c = fgetc(iter->f);
	realcolumn = iter->column + 1;
557
	if (c == '#') {
Bernhard Link's avatar
Bernhard Link committed
558 559 560
		do {
			c = fgetc(iter->f);
			realcolumn++;
561
		} while (c != '\n' && c != EOF && c != '\r');
Bernhard Link's avatar
Bernhard Link committed
562
	}
563 564
	if (c == '\r') {
		while (c == '\r') {
Bernhard Link's avatar
Bernhard Link committed
565 566 567
			realcolumn++;
			c = fgetc(iter->f);
		}
568 569 570
		if (c != '\n' && c != EOF) {
			fprintf(stderr,
"Warning parsing config file '%s', line '%u', column %u: CR not followed by LF!\n",
Bernhard Link's avatar
Bernhard Link committed
571 572 573 574 575 576
					config_filename(iter),
					config_line(iter),
					realcolumn);

		}
	}
577 578 579
	if (c == EOF) {
		fprintf(stderr,
"Warning parsing config file '%s', line '%u': File ending without final LF!\n",
Bernhard Link's avatar
Bernhard Link committed
580 581 582 583 584 585
				config_filename(iter),
				config_line(iter));
		/* fake a proper text file: */
		c = '\n';
	}
	iter->column++;
586
	if (c == '\n')
Bernhard Link's avatar
Bernhard Link committed
587 588 589 590
		iter->eol = true;
	return c;
}

Bernhard Link's avatar
Bernhard Link committed
591 592 593 594 595 596
static inline int config_nextnonspace(struct configiterator *iter) {
	int c;

	do {
		iter->markerline = iter->line;
		iter->markercolumn = iter->column;
597 598
		if (iter->eol) {
			if (!config_nextline(iter))
Bernhard Link's avatar
Bernhard Link committed
599 600
				return EOF;
		}
Bernhard Link's avatar
Bernhard Link committed
601
		c = config_nextchar(iter);
602
	} while (c == '\n' || c == ' ' || c == '\t');
Bernhard Link's avatar
Bernhard Link committed
603 604 605 606 607 608 609 610 611
	return c;
}

int config_nextnonspaceinline(struct configiterator *iter) {
	int c;

	do {
		iter->markerline = iter->line;
		iter->markercolumn = iter->column;
612
		if (iter->eol)
Bernhard Link's avatar
Bernhard Link committed
613
			return EOF;
Bernhard Link's avatar
Bernhard Link committed
614
		c = config_nextchar(iter);
615
		if (c == '\n')
Bernhard Link's avatar
Bernhard Link committed
616
			return EOF;
617
	} while (c == '\r' || c == ' ' || c == '\t');
Bernhard Link's avatar
Bernhard Link committed
618 619 620
	return c;
}

621
#define configparser_errorlast(iter, message, ...) \
Bernhard Link's avatar
Bernhard Link committed
622 623 624
	fprintf(stderr, "Error parsing %s, line %u, column %u: " message "\n", \
			iter->filename, iter->markerline, \
			iter->markercolumn, ##  __VA_ARGS__);
625
#define configparser_error(iter, message, ...) \
Bernhard Link's avatar
Bernhard Link committed
626 627 628 629
	fprintf(stderr, "Error parsing %s, line %u, column %u: " message "\n", \
			iter->filename, iter->line, \
			iter->column, ##  __VA_ARGS__);

630
retvalue config_completeword(struct configiterator *iter, char firstc, char **result_p) {
Bernhard Link's avatar
Bernhard Link committed
631 632
	size_t size = 0, len = 0;
	char *value = NULL, *nv;
633
	int c = firstc;
Bernhard Link's avatar
Bernhard Link committed
634 635 636 637

	iter->markerline = iter->line;
	iter->markercolumn = iter->column;
	do {
638
		if (len + 2 >= size) {
Bernhard Link's avatar
Bernhard Link committed
639
			nv = realloc(value, size+128);
640
			if (FAILEDTOALLOC(nv)) {
Bernhard Link's avatar
Bernhard Link committed
641 642 643 644 645 646 647 648
				free(value);
				return RET_ERROR_OOM;
			}
			size += 128;
			value = nv;
		}
		value[len] = c;
		len++;
Bernhard Link's avatar
Bernhard Link committed
649
		c = config_nextchar(iter);
650
		if (c == '\n')
Bernhard Link's avatar
Bernhard Link committed
651
			break;
652 653 654
	} while (c != ' ' && c != '\t');
	assert (len > 0);
	assert (len < size);
Bernhard Link's avatar
Bernhard Link committed
655 656
	value[len] = '\0';
	nv = realloc(value, len+1);
657
	if (nv == NULL)
Bernhard Link's avatar
Bernhard Link committed
658 659 660 661 662 663 664 665 666 667
		*result_p = value;
	else
		*result_p = nv;
	return RET_OK;
}

retvalue config_getwordinline(struct configiterator *iter, char **result_p) {
	int c;

	c = config_nextnonspaceinline(iter);
668
	if (c == EOF)
Bernhard Link's avatar
Bernhard Link committed
669 670 671 672 673 674 675 676
		return RET_NOTHING;
	return config_completeword(iter, c, result_p);
}

retvalue config_getword(struct configiterator *iter, char **result_p) {
	int c;

	c = config_nextnonspace(iter);
677
	if (c == EOF)
Bernhard Link's avatar
Bernhard Link committed
678 679 680 681
		return RET_NOTHING;
	return config_completeword(iter, c, result_p);
}

682 683
retvalue config_gettimespan(struct configiterator *iter, const char *header, unsigned long *time_p) {
	long long currentnumber, currentsum = 0;
684 685 686 687 688
	bool empty = true;
	int c;

	do {
		c = config_nextnonspace(iter);
689 690
		if (c == EOF) {
			if (empty) {
691 692 693 694 695 696 697 698 699 700
				configparser_errorlast(iter,
"Unexpected end of %s header (value expected).", header);
				return RET_ERROR;
			}
			*time_p = currentsum;
			return RET_OK;
		}
		iter->markerline = iter->line;
		iter->markercolumn = iter->column;
		currentnumber = 0;
701
		if (c < '0' || c > '9') {
702 703 704 705 706 707 708
			configparser_errorlast(iter,
"Unexpected character '%c' where a digit was expected in %s header.",
					(char)c, header);
			return RET_ERROR;
		}
		empty = false;
		do {
709 710 711 712 713
			if (currentnumber > 3660) {
				configparser_errorlast(iter,
"Absurdly long time span (> 100 years) in %s header.", header);
				return RET_ERROR;
			}
714 715 716
			currentnumber *= 10;
			currentnumber += (c - '0');
			c = config_nextchar(iter);
717 718
		} while (c >= '0' && c <= '9');
		if (c == ' ' || c == '\t' || c == '\n')
719
			c = config_nextnonspace(iter);
720
		if (c == 'y') {
721 722 723 724 725
			if (currentnumber > 100) {
				configparser_errorlast(iter,
"Absurdly long time span (> 100 years) in %s header.", header);
				return RET_ERROR;
			}
726
			currentnumber *= 365*24*60*60;
727
		} else if (c == 'm') {
728 729 730 731 732
			if (currentnumber > 1200) {
				configparser_errorlast(iter,
"Absurdly long time span (> 100 years) in %s header.", header);
				return RET_ERROR;
			}
733
			currentnumber *= 31*24*60*60;
734
		} else if (c == 'd') {
735 736 737 738 739
			if (currentnumber > 36600) {
				configparser_errorlast(iter,
"Absurdly long time span (> 100 years) in %s header.", header);
				return RET_ERROR;
			}
740 741
			currentnumber *= 24*60*60;
		} else {
742 743 744 745 746
			if (currentnumber > 36600) {
				configparser_errorlast(iter,
"Absurdly long time span (> 100 years) in %s header.", header);
				return RET_ERROR;
			}
747
			currentnumber *= 24*60*60;
748
			if (c != EOF) {
749 750 751 752 753 754 755
				configparser_errorlast(iter,
"Unexpected character '%c' where a 'd','m' or 'y' was expected in %s header.",
					(char)c, header);
				return RET_ERROR;
			}
		}
		currentsum += currentnumber;
756
	} while (true);
757 758
}

759
retvalue config_getonlyword(struct configiterator *iter, const char *header, checkfunc check, char **result_p) {
Bernhard Link's avatar
Bernhard Link committed
760 761 762 763
	char *value;
	retvalue r;

	r = config_getword(iter, &value);
764
	if (r == RET_NOTHING) {
Bernhard Link's avatar
Bernhard Link committed
765
		configparser_errorlast(iter,
Bernhard Link's avatar
Bernhard Link committed
766
"Unexpected end of %s header (value expected).", header);
Bernhard Link's avatar
Bernhard Link committed
767 768
		return RET_ERROR;
	}
769
	if (RET_WAS_ERROR(r))
Bernhard Link's avatar
Bernhard Link committed
770
		return r;
771
	if (config_nextnonspace(iter) != EOF) {
Bernhard Link's avatar
Bernhard Link committed
772
		configparser_error(iter,
Bernhard Link's avatar
Bernhard Link committed
773
"End of %s header expected (but trailing garbage).", header);
Bernhard Link's avatar
Bernhard Link committed
774 775 776
		free(value);
		return RET_ERROR;
	}
777
	if (check != NULL) {
778
		const char *errormessage = check(value);
779
		if (errormessage != NULL) {
780
			configparser_errorlast(iter,
781
"Malformed %s content '%s': %s", header, value, errormessage);
782 783 784 785 786
			free(value);
			checkerror_free(errormessage);
			return RET_ERROR;
		}
	}
Bernhard Link's avatar
Bernhard Link committed
787 788 789 790
	*result_p = value;
	return RET_OK;
}

791
retvalue config_getscript(struct configiterator *iter, const char *name, char **value_p) {
792 793 794
	char *value;
	retvalue r;

795
	r = config_getonlyword(iter, name, NULL, &value);
796 797
	if (RET_IS_OK(r)) {
		assert (value != NULL && value[0] != '\0');
798 799
		value = configfile_expandname(value, value);
		if (FAILEDTOALLOC(value))
800
			return RET_ERROR_OOM;
801
		*value_p = value;
802 803 804 805
	}
	return r;
}

806 807 808 809 810 811 812
retvalue config_geturl(struct configiterator *iter, const char *header, char **result_p) {
	char *value, *p;
	retvalue r;
	size_t l;


	r = config_getword(iter, &value);
813
	if (r == RET_NOTHING) {
814 815 816 817
		configparser_errorlast(iter,
"Unexpected end of %s header (value expected).", header);
		return RET_ERROR;
	}
818
	if (RET_WAS_ERROR(r))
819 820
		return r;
	// TODO: think about allowing (escaped) spaces...
821
	if (config_nextnonspace(iter) != EOF) {
822 823 824 825 826 827
		configparser_error(iter,
"End of %s header expected (but trailing garbage).", header);
		free(value);
		return RET_ERROR;
	}
	p = value;
828
	while (*p != '\0' && (*p == '_' || *p == '-' || *p == '+' ||
829
				(*p>='a' && *p<='z') || (*p>='A' && *p<='Z') ||
830
				(*p>='0' && *p<='9'))) {
831 832
		p++;
	}
833
	if (*p != ':') {
834 835 836 837 838
		configparser_errorlast(iter,
"Malformed %s field: no colon (must be method:path).", header);
		free(value);
		return RET_ERROR;
	}
839
	if (p == value) {
840 841 842 843 844 845 846 847 848
		configparser_errorlast(iter,
"Malformed %s field: transport method name expected (colon is not allowed to be the first character)!", header);
		free(value);
		return RET_ERROR;
	}
	p++;
	l = strlen(p);
	/* remove one leading slash, as we always add one and some apt-methods
	 * are confused with //. (end with // if you really want it) */
849
	if (l > 0 && p[l - 1] == '/')
850 851 852 853 854
		p[l - 1] = '\0';
	*result_p = value;
	return RET_OK;
}

855
retvalue config_getuniqwords(struct configiterator *iter, const char *header, checkfunc check, struct strlist *result_p) {
Bernhard Link's avatar
Bernhard Link committed
856 857 858
	char *value;
	retvalue r;
	struct strlist data;
859
	const char *errormessage;
Bernhard Link's avatar
Bernhard Link committed
860 861

	strlist_init(&data);
862 863
	while ((r = config_getword(iter, &value)) != RET_NOTHING) {
		if (RET_WAS_ERROR(r)) {
Bernhard Link's avatar
Bernhard Link committed
864 865 866
			strlist_done(&data);
			return r;
		}
867
		if (strlist_in(&data, value)) {
Bernhard Link's avatar
Bernhard Link committed
868
			configparser_errorlast(iter,
Bernhard Link's avatar
Bernhard Link committed
869
"Unexpected duplicate '%s' within %s header.", value, header);
Bernhard Link's avatar
Bernhard Link committed
870 871 872
			free(value);
			strlist_done(&data);
			return RET_ERROR;
873
		} else if (check != NULL && (errormessage = check(value)) != NULL) {
874
			configparser_errorlast(iter,
875
"Malformed %s element '%s': %s", header, value, errormessage);
876 877
			checkerror_free(errormessage);
			free(value);
878
			strlist_done(&data);
879
			return RET_ERROR;
Bernhard Link's avatar
Bernhard Link committed
880 881
		} else {
			r = strlist_add(&data, value);
882
			if (RET_WAS_ERROR(r)) {
Bernhard Link's avatar
Bernhard Link committed
883 884 885 886 887 888 889 890 891
				strlist_done(&data);
				return r;
			}
		}
	}
	strlist_move(result_p, &data);
	return RET_OK;
}

892 893 894 895 896 897 898 899
retvalue config_getinternatomlist(struct configiterator *iter, const char *header, enum atom_type type, checkfunc check, struct atomlist *result_p) {
	char *value;
	retvalue r;
	struct atomlist data;
	const char *errormessage;
	atom_t atom;

	atomlist_init(&data);
900 901
	while ((r = config_getword(iter, &value)) != RET_NOTHING) {
		if (RET_WAS_ERROR(r)) {
902 903 904
			atomlist_done(&data);
			return r;
		}
905
		if (check != NULL && (errormessage = check(value)) != NULL) {
906
			configparser_errorlast(iter,
907
"Malformed %s element '%s': %s", header, value, errormessage);
908 909 910 911 912 913
			checkerror_free(errormessage);
			free(value);
			atomlist_done(&data);
			return RET_ERROR;
		}
		r = atom_intern(type, value, &atom);
914
		if (RET_WAS_ERROR(r))
915 916
			return r;
		r = atomlist_add_uniq(&data, atom);
917
		if (r == RET_NOTHING) {
918 919 920 921 922 923 924
			configparser_errorlast(iter,
"Unexpected duplicate '%s' within %s header.", value, header);
			free(value);
			atomlist_done(&data);
			return RET_ERROR;
		}
		free(value);
925
		if (RET_WAS_ERROR(r)) {
926 927 928 929 930 931 932 933
			atomlist_done(&data);
			return r;
		}
	}
	atomlist_move(result_p, &data);
	return RET_OK;
}

934 935 936 937 938 939
retvalue config_getatom(struct configiterator *iter, const char *header, enum atom_type type, atom_t *result_p) {
	char *value;
	retvalue r;
	atom_t atom;

	r = config_getword(iter, &value);
940
	if (r == RET_NOTHING) {
941 942 943 944
		configparser_errorlast(iter,
"Unexpected empty '%s' field.", header);
		r = RET_ERROR_MISSING;
	}
945
	if (RET_WAS_ERROR(r))
946 947
		return r;
	atom = atom_find(type, value);
948
	if (!atom_defined(atom)) {
949 950 951 952 953 954 955 956 957 958
		configparser_errorlast(iter,
"Not previously seen %s '%s' within '%s' field.", atomtypes[type], value, header);
		free(value);
		return RET_ERROR;
	}
	*result_p = atom;
	free(value);
	return RET_OK;
}

959 960 961 962 963 964 965
retvalue config_getatomlist(struct configiterator *iter, const char *header, enum atom_type type, struct atomlist *result_p) {
	char *value;
	retvalue r;
	struct atomlist data;
	atom_t atom;

	atomlist_init(&data);
966 967
	while ((r = config_getword(iter, &value)) != RET_NOTHING) {
		if (RET_WAS_ERROR(r)) {
968 969 970 971
			atomlist_done(&data);
			return r;
		}
		atom = atom_find(type, value);
972
		if (!atom_defined(atom)) {
973 974 975 976 977 978 979
			configparser_errorlast(iter,
"Not previously seen %s '%s' within '%s' header.", atomtypes[type], value, header);
			free(value);
			atomlist_done(&data);
			return RET_ERROR;
		}
		r = atomlist_add_uniq(&data, atom);
980
		if (r == RET_NOTHING) {
981 982 983 984 985 986 987
			configparser_errorlast(iter,
"Unexpected duplicate '%s' within %s header.", value, header);
			free(value);
			atomlist_done(&data);
			return RET_ERROR;
		}
		free(value);
988
		if (RET_WAS_ERROR(r)) {
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
			atomlist_done(&data);
			return r;
		}
	}
	atomlist_move(result_p, &data);
	return RET_OK;
}

retvalue config_getsplitatoms(struct configiterator *iter, const char *header, enum atom_type type, struct atomlist *from_p, struct atomlist *into_p) {
	char *value, *separator;
	atom_t origin, destination;
	retvalue r;
	struct atomlist data_from, data_into;

	atomlist_init(&data_from);
	atomlist_init(&data_into);
1005 1006
	while ((r = config_getword(iter, &value)) != RET_NOTHING) {
		if (RET_WAS_ERROR(r)) {
1007 1008 1009 1010 1011
			atomlist_done(&data_from);
			atomlist_done(&data_into);
			return r;
		}
		separator = strchr(value, '>');
1012
		if (separator == NULL) {
1013 1014 1015
			separator = value;
			destination = atom_find(type, value);
			origin = destination;;
1016
		} else if (separator == value) {
1017 1018
			destination = atom_find(type, separator + 1);
			origin = destination;;
1019
		} else if (separator[1] == '\0') {
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
			*separator = '\0';
			separator = value;
			destination = atom_find(type, value);
			origin = destination;;
		} else {
			*separator = '\0';
			separator++;
			origin = atom_find(type, value);
			destination = atom_find(type, separator);
		}
1030
		if (!atom_defined(origin)) {
1031 1032 1033 1034 1035 1036 1037
			configparser_errorlast(iter,
"Unknown %s '%s' in %s.", atomtypes[type], value, header);
			free(value);
			atomlist_done(&data_from);
			atomlist_done(&data_into);
			return RET_ERROR;
		}
1038
		if (!atom_defined(destination)) {
1039 1040 1041 1042 1043 1044 1045 1046 1047
			configparser_errorlast(iter,
"Unknown %s '%s' in %s.", atomtypes[type], separator, header);
			free(value);
			atomlist_done(&data_from);
			atomlist_done(&data_into);
			return RET_ERROR;
		}
		free(value);
		r = atomlist_add(&data_from, origin);
1048
		if (RET_WAS_ERROR(r)) {
1049 1050 1051 1052 1053
			atomlist_done(&data_from);
			atomlist_done(&data_into);
			return r;
		}
		r = atomlist_add(&data_into, destination);
1054
		if (RET_WAS_ERROR(r)) {
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
			atomlist_done(&data_from);
			atomlist_done(&data_into);
			return r;
		}
	}
	atomlist_move(from_p, &data_from);
	atomlist_move(into_p, &data_into);
	return RET_OK;
}

retvalue config_getatomsublist(struct configiterator *iter, const char *header, enum atom_type type, struct atomlist *result_p, const struct atomlist *superset, const char *superset_header) {
	char *value;
	retvalue r;
	struct atomlist data;
	atom_t atom;

	atomlist_init(&data);
1072 1073
	while ((r = config_getword(iter, &value)) != RET_NOTHING) {
		if (RET_WAS_ERROR(r)) {
1074 1075 1076 1077
			atomlist_done(&data);
			return r;
		}
		atom = atom_find(type, value);
1078
		if (!atom_defined(atom) || !atomlist_in(superset, atom)) {
1079
			configparser_errorlast(iter,
1080
"'%s' not allowed in %s as it was not in %s.", value, header, superset_header);
1081 1082 1083 1084 1085
			free(value);
			atomlist_done(&data);
			return RET_ERROR;
		}
		r = atomlist_add_uniq(&data, atom);
1086
		if (r == RET_NOTHING) {
1087 1088 1089 1090 1091 1092 1093
			configparser_errorlast(iter,
"Unexpected duplicate '%s' within %s header.", value, header);
			free(value);
			atomlist_done(&data);
			return RET_ERROR;
		}
		free(value);
1094
		if (RET_WAS_ERROR(r)) {
1095 1096 1097 1098 1099 1100 1101 1102
			atomlist_done(&data);
			return r;
		}
	}
	atomlist_move(result_p, &data);
	return RET_OK;
}

Bernhard Link's avatar
Bernhard Link committed
1103 1104 1105 1106 1107 1108
retvalue config_getwords(struct configiterator *iter, struct strlist *result_p) {
	char *value;
	retvalue r;
	struct strlist data;

	strlist_init(&data);
1109 1110
	while ((r = config_getword(iter, &value)) != RET_NOTHING) {
		if (RET_WAS_ERROR(r)) {
Bernhard Link's avatar
Bernhard Link committed
1111 1112 1113 1114
			strlist_done(&data);
			return r;
		}
		r = strlist_add(&data, value);
1115
		if (RET_WAS_ERROR(r)) {
Bernhard Link's avatar
Bernhard Link committed
1116 1117 1118
			strlist_done(&data);
			return r;
		}
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
	}
	strlist_move(result_p, &data);
	return RET_OK;
}

retvalue config_getsignwith(struct configiterator *iter, const char *name, struct strlist *result_p) {
	char *value;
	retvalue r;
	struct strlist data;
	int c;

	strlist_init(&data);

	c = config_nextnonspace(iter);
1133
	if (c == EOF) {
1134 1135 1136 1137 1138
		configparser_errorlast(iter,
"Missing value for %s field.", name);
		return RET_ERROR;
	}
	/* if the first character is a '!', a script to start follows */
1139
	if (c == '!') {
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
		const char *type = "!";

		iter->markerline = iter->line;
		iter->markercolumn = iter->column;
		c = config_nextchar(iter);
		if (c == '-') {
			configparser_errorlast(iter,
"'!-' in signwith lines reserved for future usage!\n");
			return RET_ERROR;
			type = "!-";
			c = config_nextnonspace(iter);
		} else if (c == '\n' || c == ' ' || c == '\t')
			c = config_nextnonspace(iter);
		if (c == EOF) {
			configparser_errorlast(iter,
"Missing value for %s field.", name);
			return RET_ERROR;
		}
		r = config_completeword(iter, c, &value);
1159
		if (RET_WAS_ERROR(r))
1160
			return r;
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
		if (config_nextnonspace(iter) != EOF) {
			configparser_error(iter,
"End of %s header expected (but trailing garbage).", name);
			free(value);
			return RET_ERROR;
		}
		assert (value != NULL && value[0] != '\0');
		value = configfile_expandname(value, value);
		if (FAILEDTOALLOC(value))
			return RET_ERROR_OOM;
		r = strlist_add_dup(&data, type);
1172
		if (RET_WAS_ERROR(r)) {
1173
			free(value);
1174 1175 1176
			return r;
		}
		r = strlist_add(&data, value);
1177
		if (RET_WAS_ERROR(r)) {
1178 1179 1180 1181 1182 1183 1184 1185
			strlist_done(&data);
			return r;
		}
		strlist_move(result_p, &data);
		return RET_OK;
	}
	/* otherwise each word is stored in the strlist */
	r = config_completeword(iter, c, &value);
1186 1187
	assert (r != RET_NOTHING);
	if (RET_WAS_ERROR(r))
1188 1189
		return r;
	r = strlist_add(&data, value);
1190
	if (RET_WAS_ERROR(r))
1191
		return r;
1192 1193
	while ((r = config_getword(iter, &value)) != RET_NOTHING) {
		if (RET_WAS_ERROR(r)) {
1194 1195 1196 1197
			strlist_done(&data);
			return r;
		}
		r = strlist_add(&data, value);
1198
		if (RET_WAS_ERROR(r)) {
1199 1200 1201
			strlist_done(&data);
			return r;
		}
Bernhard Link's avatar
Bernhard Link committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
	}
	strlist_move(result_p, &data);
	return RET_OK;
}

retvalue config_getsplitwords(struct configiterator *iter, UNUSED(const char *header), struct strlist *from_p, struct strlist *into_p) {
	char *value, *origin, *destination, *separator;
	retvalue r;
	struct strlist data_from, data_into;

	strlist_init(&data_from);
	strlist_init(&data_into);
1214 1215
	while ((r = config_getword(iter, &value)) != RET_NOTHING) {
		if (RET_WAS_ERROR(r)) {
Bernhard Link's avatar
Bernhard Link committed
1216 1217 1218 1219 1220
			strlist_done(&data_from);
			strlist_done(&data_into);
			return r;
		}
		separator = strchr(value, '>');
1221
		if (separator == NULL) {
Bernhard Link's avatar
Bernhard Link committed
1222 1223
			destination = strdup(value);
			origin = value;
1224
		} else if (separator == value) {
Bernhard Link's avatar
Bernhard Link committed
1225 1226 1227
			destination = strdup(separator+1);
			origin = strdup(separator+1);
			free(value);
1228
		} else if (separator[1] == '\0') {
Bernhard Link's avatar
Bernhard Link committed
1229 1230 1231 1232 1233 1234 1235 1236
			*separator = '\0';
			destination = strdup(value);
			origin = value;
		} else {
			origin = strndup(value, separator-value);
			destination = strdup(separator+1);
			free(value);
		}
1237 1238
		if (FAILEDTOALLOC(origin) || FAILEDTOALLOC(destination)) {
			free(origin); free(destination);
Bernhard Link's avatar
Bernhard Link committed
1239 1240 1241 1242 1243
			strlist_done(&data_from);
			strlist_done(&data_into);
			return RET_ERROR_OOM;
		}
		r = strlist_add(&data_from, origin);
1244
		if (RET_WAS_ERROR(r)) {
Bernhard Link's avatar
Bernhard Link committed
1245 1246 1247 1248 1249 1250
			free(destination);
			strlist_done(&data_from);
			strlist_done(&data_into);
			return r;
		}
		r = strlist_add(&data_into, destination);
1251
		if (RET_WAS_ERROR(r)) {
Bernhard Link's avatar
Bernhard Link committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
			strlist_done(&data_from);
			strlist_done(&data_into);
			return r;
		}
	}
	strlist_move(from_p, &data_from);
	strlist_move(into_p, &data_into);
	return RET_OK;
}

retvalue config_getconstant(struct configiterator *iter, const struct constant *constants, int *result_p) {
	retvalue r;
	char *value;
	const struct constant *c;

1267 1268
	/* that could be done more in-situ,
	 * but is not runtime-critical at all */
Bernhard Link's avatar
Bernhard Link committed
1269 1270

	r = config_getword(iter, &value);
1271
	if (r == RET_NOTHING)
Bernhard Link's avatar
Bernhard Link committed
1272
		return r;
1273
	if (RET_WAS_ERROR(r))
Bernhard Link's avatar
Bernhard Link committed
1274
		return r;
1275 1276
	for (c = constants ; c->name != NULL ; c++) {
		if (strcmp(c->name, value) == 0) {
Bernhard Link's avatar
Bernhard Link committed
1277 1278 1279 1280 1281 1282 1283 1284 1285
			free(value);
			*result_p = c->value;
			return RET_OK;
		}
	}
	free(value);
	return RET_ERROR_UNKNOWNFIELD;
}

1286
retvalue config_getflags(struct configiterator *iter, const char *header, const struct constant *constants, bool *flags, bool ignoreunknown, const char *msg) {
1287
	retvalue r, result = RET_NOTHING;
Bernhard Link's avatar
Bernhard Link committed
1288 1289
	int option = -1;

1290 1291 1292 1293 1294
	while (true) {
		r = config_getconstant(iter, constants, &option);
		if (r == RET_NOTHING)
			break;
		if (r == RET_ERROR_UNKNOWNFIELD) {
Bernhard Link's avatar
Bernhard Link committed
1295
// TODO: would be nice to have the wrong flag here to put it in the error message:
1296
			if (ignoreunknown) {
Bernhard Link's avatar
Bernhard Link committed
1297 1298
				fprintf(stderr,
"Warning: ignored error parsing config file %s, line %u, column %u:\n"
Bernhard Link's avatar
Bernhard Link committed
1299
"Unknown flag in %s header.%s\n",
Bernhard Link's avatar
Bernhard Link committed
1300 1301 1302 1303 1304 1305 1306 1307
					config_filename(iter),
					config_markerline(iter),
					config_markercolumn(iter),
					header, msg);
				continue;
			}
			fprintf(stderr,
"Error parsing config file %s, line %u, column %u:\n"
Bernhard Link's avatar
Bernhard Link committed
1308
"Unknown flag in %s header.%s\n",
Bernhard Link's avatar
Bernhard Link committed
1309 1310 1311 1312 1313
					config_filename(iter),
					config_markerline(iter),
					config_markercolumn(iter),
					header, msg);
		}
1314
		if (RET_WAS_ERROR(r))
Bernhard Link's avatar
Bernhard Link committed
1315
			return r;
1316
		assert (option >= 0);
1317
		flags[option] = true;
Bernhard Link's avatar
Bernhard Link committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
		result = RET_OK;
		option = -1;
	}
	return result;
}

retvalue config_getall(struct configiterator *iter, char **result_p) {
	size_t size = 0, len = 0;
	char *value = NULL, *nv;
	int c;

	c = config_nextnonspace(iter);
1330
	if (c == EOF)
Bernhard Link's avatar
Bernhard Link committed
1331 1332 1333 1334
		return RET_NOTHING;
	iter->markerline = iter->line;
	iter->markercolumn = iter->column;
	do {
1335
		if (len + 2 >= size) {
Bernhard Link's avatar
Bernhard Link committed
1336
			nv = realloc(value, size+128);
1337
			if (FAILEDTOALLOC(nv)) {
Bernhard Link's avatar
Bernhard Link committed
1338 1339 1340 1341 1342 1343 1344 1345
				free(value);
				return RET_ERROR_OOM;
			}
			size += 128;
			value = nv;
		}
		value[len] = c;
		len++;
1346 1347
		if (iter->eol) {
			if (!config_nextline(iter))
Bernhard Link's avatar
Bernhard Link committed
1348 1349
				break;
		}
Bernhard Link's avatar
Bernhard Link committed
1350
		c = config_nextchar(iter);
1351 1352 1353 1354 1355
	} while (true);
	assert (len > 0);
	assert (len < size);
	while (len > 0 && (value[len-1] == ' ' || value[len-1] == '\t' ||
			    value[len-1] == '\n' || value[len-1] == '\r'))
Bernhard Link's avatar
Bernhard Link committed
1356 1357 1358
		len--;
	value[len] = '\0';
	nv = realloc(value, len+1);
1359
	if (nv == NULL)
Bernhard Link's avatar
Bernhard Link committed
1360 1361 1362 1363 1364 1365
		*result_p = value;
	else
		*result_p = nv;
	return RET_OK;
}

1366
retvalue config_gettruth(struct configiterator *iter, const char *header, bool *result_p) {
Bernhard Link's avatar
Bernhard Link committed
1367 1368 1369 1370 1371 1372
	char *value = NULL;
	retvalue r;

	/* wastefull, but does not happen that often */

	r = config_getword(iter, &value);
1373
	if (r == RET_NOTHING) {
Bernhard Link's avatar
Bernhard Link committed
1374
		configparser_errorlast(iter,
Bernhard Link's avatar
Bernhard Link committed
1375
"Unexpected empty boolean %s header (something like Yes or No expected).", header);
Bernhard Link's avatar
Bernhard Link committed
1376 1377
		return RET_ERROR;
	}
1378
	if (RET_WAS_ERROR(r))
Bernhard Link's avatar
Bernhard Link committed
1379 1380
		return r;
	// TODO: check against trailing garbage
1381
	if (strcasecmp(value, "Yes") == 0) {
1382
		*result_p = true;
Bernhard Link's avatar
Bernhard Link committed
1383 1384 1385
		free(value);
		return RET_OK;
	}
1386
	if (strcasecmp(value, "No") == 0) {
1387
		*result_p = false;
Bernhard Link's avatar
Bernhard Link committed
1388 1389 1390
		free(value);
		return RET_OK;
	}
1391
	if (strcmp(value, "1") == 0) {
1392
		*result_p = true;
Bernhard Link's avatar
Bernhard Link committed
1393 1394 1395
		free(value);
		return RET_OK;
	}
1396
	if (strcmp(value, "0") == 0) {
1397
		*result_p = false;
Bernhard Link's avatar
Bernhard Link committed
1398 1399 1400 1401
		free(value);
		return RET_OK;
	}
	configparser_errorlast(iter,
Bernhard Link's avatar
Bernhard Link committed
1402
"Unexpected value in boolean %s header (something like Yes or No expected).", header);
Bernhard Link's avatar
Bernhard Link committed
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	free(value);
	return RET_ERROR;
}

retvalue config_getnumber(struct configiterator *iter, const char *name, long long *result_p, long long minval, long long maxval) {
	char *word = NULL;
	retvalue r;
	long long value;
	char *e;

	r = config_getword(iter, &word);
1414
	if (r == RET_NOTHING) {
Bernhard Link's avatar
Bernhard Link committed
1415 1416 1417 1418
		configparser_errorlast(iter,
"Unexpected end of line (%s number expected).", name);
		return RET_ERROR;
	}
1419
	if (RET_WAS_ERROR(r))
Bernhard Link's avatar
Bernhard Link committed
1420 1421 1422
		return r;

	value = strtoll(word, &e, 10);
1423
	if (e == word) {
Bernhard Link's avatar
Bernhard Link committed
1424 1425 1426 1427 1428 1429 1430 1431
		fprintf(stderr,
"Error parsing config file %s, line %u, column %u:\n"
"Expected %s number but got '%s'\n",
			config_filename(iter), config_markerline(iter),
			config_markercolumn(iter), name, word);
		free(word);
		return RET_ERROR;
	}
1432
	if (e != NULL && *e != '\0') {
Bernhard Link's avatar
Bernhard Link committed
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
		unsigned char digit1, digit2, digit3;
		digit1 = ((unsigned char)(*e))&0x7;
		digit2 = (((unsigned char)(*e)) >> 3)&0x7;
		digit3 = (((unsigned char)(*e)) >> 6)&0x7;
		fprintf(stderr,
"Error parsing config file %s, line %u, column %u:\n"
"Unexpected character \\%01hhu%01hhu%01hhu in %s number '%s'\n",
			config_filename(iter), config_markerline(iter),
			config_markercolumn(iter) + (int)(e-word),
			digit3, digit2, digit1,
			name, word);
		free(word);
		return RET_ERROR;
	}
1447
	if (value == LLONG_MAX || value > maxval) {
Bernhard Link's avatar
Bernhard Link committed
1448 1449 1450 1451 1452 1453 1454 1455
		fprintf(stderr,
"Error parsing config file %s, line %u, column %u:\n"
"Too large %s number '%s'\n",
			config_filename(iter), config_markerline(iter),
			config_markercolumn(iter), name, word);
		free(word);
		return RET_ERROR;
	}
1456
	if (value == LLONG_MIN || value < minval) {
Bernhard Link's avatar
Bernhard Link committed
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
		fprintf(stderr,
"Error parsing config file %s, line %u, column %u:\n"
"Too small %s number '%s'\n",
			config_filename(iter), config_markerline(iter),
			config_markercolumn(iter), name, word);
		free(word);
		return RET_ERROR;
	}
	free(word);
	*result_p = value;
	return RET_OK;
}
1469

1470
static retvalue config_getline(struct configiterator *iter, /*@out@*/char **result_p) {
1471 1472 1473 1474 1475
	size_t size = 0, len = 0;
	char *value = NULL, *nv;
	int c;

	c = config_nextnonspace(iter);
1476
	if (c == EOF)
1477