config_file.c 31.4 KB
Newer Older
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8 9 10
 */

#include "common.h"
#include "config.h"
#include "fileops.h"
11
#include "filebuf.h"
12
#include "buffer.h"
13
#include "git2/config.h"
14
#include "git2/types.h"
15
#include "strmap.h"
16

17
#include <ctype.h>
18 19
#include <sys/types.h>
#include <regex.h>
20

21
GIT__USE_STRMAP;
22

23 24
typedef struct cvar_t {
	struct cvar_t *next;
25
	git_config_entry *entry;
26
} cvar_t;
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

#define CVAR_LIST_HEAD(list) ((list)->head)

#define CVAR_LIST_TAIL(list) ((list)->tail)

#define CVAR_LIST_NEXT(var) ((var)->next)

#define CVAR_LIST_EMPTY(list) ((list)->head == NULL)

#define CVAR_LIST_APPEND(list, var) do {\
	if (CVAR_LIST_EMPTY(list)) {\
		CVAR_LIST_HEAD(list) = CVAR_LIST_TAIL(list) = var;\
	} else {\
		CVAR_LIST_NEXT(CVAR_LIST_TAIL(list)) = var;\
		CVAR_LIST_TAIL(list) = var;\
	}\
} while(0)

#define CVAR_LIST_REMOVE_HEAD(list) do {\
	CVAR_LIST_HEAD(list) = CVAR_LIST_NEXT(CVAR_LIST_HEAD(list));\
} while(0)

#define CVAR_LIST_REMOVE_AFTER(var) do {\
	CVAR_LIST_NEXT(var) = CVAR_LIST_NEXT(CVAR_LIST_NEXT(var));\
} while(0)

#define CVAR_LIST_FOREACH(list, iter)\
	for ((iter) = CVAR_LIST_HEAD(list);\
		 (iter) != NULL;\
		 (iter) = CVAR_LIST_NEXT(iter))

/*
 * Inspired by the FreeBSD functions
 */
#define CVAR_LIST_FOREACH_SAFE(start, iter, tmp)\
	for ((iter) = CVAR_LIST_HEAD(vars);\
		 (iter) && (((tmp) = CVAR_LIST_NEXT(iter) || 1));\
		 (iter) = (tmp))

typedef struct {
67
	git_config_file parent;
68

69
	git_strmap *values;
70 71

	struct {
72
		git_buf buffer;
73 74 75 76 77 78
		char *read_ptr;
		int line_number;
		int eof;
	} reader;

	char *file_path;
79
} diskfile_backend;
80

81
static int config_parse(diskfile_backend *cfg_file, unsigned int level);
82
static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_value);
83
static int config_write(diskfile_backend *cfg, const char *key, const regex_t *preg, const char *value);
84
static char *escape_value(const char *ptr);
85

86 87 88 89 90 91
static void set_parse_error(diskfile_backend *backend, int col, const char *error_str)
{
	giterr_set(GITERR_CONFIG, "Failed to parse config file: %s (in %s:%d, column %d)",
		error_str, backend->file_path, backend->reader.line_number, col);
}

92
static void cvar_free(cvar_t *var)
93 94 95 96
{
	if (var == NULL)
		return;

97 98 99
	git__free((char*)var->entry->name);
	git__free((char *)var->entry->value);
	git__free(var->entry);
100
	git__free(var);
101 102
}

103 104
/* Take something the user gave us and make it nice for our hash function */
static int normalize_name(const char *in, char **out)
105
{
106
	char *name, *fdot, *ldot;
107

108
	assert(in && out);
109

110
	name = git__strdup(in);
111
	GITERR_CHECK_ALLOC(name);
112

113 114
	fdot = strchr(name, '.');
	ldot = strrchr(name, '.');
115

116 117
	if (fdot == NULL || ldot == NULL) {
		git__free(name);
118 119 120
		giterr_set(GITERR_CONFIG,
			"Invalid variable name: '%s'", in);
		return -1;
121
	}
122

123 124 125 126 127
	/* Downcase up to the first dot and after the last one */
	git__strntolower(name, fdot - name);
	git__strtolower(ldot);

	*out = name;
128
	return 0;
129 130
}

131
static void free_vars(git_strmap *values)
132
{
133
	cvar_t *var = NULL;
134

135 136
	if (values == NULL)
		return;
137

138
	git_strmap_foreach_value(values, var,
139 140 141 142 143
		while (var != NULL) {
			cvar_t *next = CVAR_LIST_NEXT(var);
			cvar_free(var);
			var = next;
		});
144

145
	git_strmap_free(values);
146 147
}

148
static int config_open(git_config_file *cfg, unsigned int level)
149
{
150
	int res;
151
	diskfile_backend *b = (diskfile_backend *)cfg;
152

153
	b->values = git_strmap_alloc();
154
	GITERR_CHECK_ALLOC(b->values);
155

156
	git_buf_init(&b->reader.buffer, 0);
157
	res = git_futils_readbuffer(&b->reader.buffer, b->file_path);
158

159
	/* It's fine if the file doesn't exist */
160 161 162
	if (res == GIT_ENOTFOUND)
		return 0;

163
	if (res < 0 || config_parse(b, level) <  0) {
164 165 166 167 168
		free_vars(b->values);
		b->values = NULL;
		git_buf_free(&b->reader.buffer);
		return -1;
	}
169

170
	git_buf_free(&b->reader.buffer);
171
	return 0;
172 173
}

174
static void backend_free(git_config_file *_backend)
175
{
176
	diskfile_backend *backend = (diskfile_backend *)_backend;
177 178 179 180

	if (backend == NULL)
		return;

181
	git__free(backend->file_path);
182
	free_vars(backend->values);
183
	git__free(backend);
184 185
}

186 187 188
static int file_foreach(
	git_config_file *backend,
	const char *regexp,
189
	int (*fn)(const git_config_entry *, void *),
190
	void *data)
191
{
192
	diskfile_backend *b = (diskfile_backend *)backend;
193
	cvar_t *var, *next_var;
194
	const char *key;
195 196
	regex_t regex;
	int result = 0;
197

198 199
	if (!b->values)
		return 0;
200

201 202 203 204 205 206 207
	if (regexp != NULL) {
		if ((result = regcomp(&regex, regexp, REG_EXTENDED)) < 0) {
			giterr_set_regex(&regex, result);
			regfree(&regex);
			return -1;
		}
	}
208

209
	git_strmap_foreach(b->values, key, var,
210 211 212
		for (; var != NULL; var = next_var) {
			next_var = CVAR_LIST_NEXT(var);

213 214 215 216 217
			/* skip non-matching keys if regexp was provided */
			if (regexp && regexec(&regex, key, 0, NULL, 0) != 0)
				continue;

			/* abort iterator on non-zero return value */
218
			if (fn(var->entry, data)) {
Russell Belfer committed
219
				giterr_clear();
220
				result = GIT_EUSER;
221
				goto cleanup;
222
			}
223
		}
224
	);
225

226 227 228 229 230
cleanup:
	if (regexp != NULL)
		regfree(&regex);

	return result;
231 232
}

233
static int config_set(git_config_file *cfg, const char *name, const char *value)
234
{
235
	cvar_t *var = NULL, *old_var;
236
	diskfile_backend *b = (diskfile_backend *)cfg;
237
	char *key, *esc_value = NULL;
238
	khiter_t pos;
239
	int rval, ret;
240

241 242
	if (normalize_name(name, &key) < 0)
		return -1;
243 244

	/*
245 246
	 * Try to find it in the existing values and update it if it
	 * only has one value.
247
	 */
248 249 250
	pos = git_strmap_lookup_index(b->values, key);
	if (git_strmap_valid_index(b->values, pos)) {
		cvar_t *existing = git_strmap_value_at(b->values, pos);
251
		char *tmp = NULL;
252 253

		git__free(key);
Russell Belfer committed
254

255 256 257 258
		if (existing->next != NULL) {
			giterr_set(GITERR_CONFIG, "Multivar incompatible with simple set");
			return -1;
		}
259

Russell Belfer committed
260
		/* don't update if old and new values already match */
261 262
		if ((!existing->entry->value && !value) ||
			(existing->entry->value && value && !strcmp(existing->entry->value, value)))
Russell Belfer committed
263 264
			return 0;

265 266 267
		if (value) {
			tmp = git__strdup(value);
			GITERR_CHECK_ALLOC(tmp);
268 269
			esc_value = escape_value(value);
			GITERR_CHECK_ALLOC(esc_value);
270
		}
271

272 273
		git__free((void *)existing->entry->value);
		existing->entry->value = tmp;
274

275
		ret = config_write(b, existing->entry->name, NULL, esc_value);
276 277 278

		git__free(esc_value);
		return ret;
279 280
	}

281
	var = git__malloc(sizeof(cvar_t));
282
	GITERR_CHECK_ALLOC(var);
283
	memset(var, 0x0, sizeof(cvar_t));
284 285 286
	var->entry = git__malloc(sizeof(git_config_entry));
	GITERR_CHECK_ALLOC(var->entry);
	memset(var->entry, 0x0, sizeof(git_config_entry));
287

288 289
	var->entry->name = key;
	var->entry->value = NULL;
290

291
	if (value) {
292 293
		var->entry->value = git__strdup(value);
		GITERR_CHECK_ALLOC(var->entry->value);
294 295
		esc_value = escape_value(value);
		GITERR_CHECK_ALLOC(esc_value);
296 297
	}

298 299
	if (config_write(b, key, NULL, esc_value) < 0) {
		git__free(esc_value);
300 301 302 303
		cvar_free(var);
		return -1;
	}

304
	git__free(esc_value);
305
	git_strmap_insert2(b->values, key, var, old_var, rval);
306
	if (rval < 0)
307
		return -1;
308 309
	if (old_var != NULL)
		cvar_free(old_var);
310

311
	return 0;
312 313 314 315 316
}

/*
 * Internal function that actually gets the value in string form
 */
317
static int config_get(git_config_file *cfg, const char *name, const git_config_entry **out)
318
{
319
	diskfile_backend *b = (diskfile_backend *)cfg;
320
	char *key;
321
	khiter_t pos;
322

323 324
	if (normalize_name(name, &key) < 0)
		return -1;
325

326
	pos = git_strmap_lookup_index(b->values, key);
327
	git__free(key);
328

329
	/* no error message; the config system will write one */
330
	if (!git_strmap_valid_index(b->values, pos))
331
		return GIT_ENOTFOUND;
332

333
	*out = ((cvar_t *)git_strmap_value_at(b->values, pos))->entry;
334

335
	return 0;
336 337
}

338 339 340 341
static int config_get_multivar(
	git_config_file *cfg,
	const char *name,
	const char *regex_str,
342
	int (*fn)(const git_config_entry *, void *),
343
	void *data)
344 345 346 347
{
	cvar_t *var;
	diskfile_backend *b = (diskfile_backend *)cfg;
	char *key;
348
	khiter_t pos;
349

350 351
	if (normalize_name(name, &key) < 0)
		return -1;
352

353
	pos = git_strmap_lookup_index(b->values, key);
354 355
	git__free(key);

356
	if (!git_strmap_valid_index(b->values, pos))
357
		return GIT_ENOTFOUND;
358

359
	var = git_strmap_value_at(b->values, pos);
360

361 362 363
	if (regex_str != NULL) {
		regex_t regex;
		int result;
364

365 366 367 368
		/* regex matching; build the regex */
		result = regcomp(&regex, regex_str, REG_EXTENDED);
		if (result < 0) {
			giterr_set_regex(&regex, result);
369
			regfree(&regex);
370
			return -1;
371 372
		}

373 374 375
		/* and throw the callback only on the variables that
		 * match the regex */
		do {
376
			if (regexec(&regex, var->entry->value, 0, NULL, 0) == 0) {
377 378
				/* early termination by the user is not an error;
				 * just break and return successfully */
379
				if (fn(var->entry, data) < 0)
380 381
					break;
			}
382

383 384
			var = var->next;
		} while (var != NULL);
385
		regfree(&regex);
386 387 388 389 390
	} else {
		/* no regex; go through all the variables */
		do {
			/* early termination by the user is not an error;
			 * just break and return successfully */
391
			if (fn(var->entry, data) < 0)
392 393 394 395 396
				break;

			var = var->next;
		} while (var != NULL);
	}
397

398
	return 0;
399 400
}

401 402
static int config_set_multivar(
	git_config_file *cfg, const char *name, const char *regexp, const char *value)
403
{
404
	int replaced = 0;
405
	cvar_t *var, *newvar;
406 407 408
	diskfile_backend *b = (diskfile_backend *)cfg;
	char *key;
	regex_t preg;
409
	int result;
410
	khiter_t pos;
411

412
	assert(regexp);
413

414 415
	if (normalize_name(name, &key) < 0)
		return -1;
416

417 418
	pos = git_strmap_lookup_index(b->values, key);
	if (!git_strmap_valid_index(b->values, pos)) {
419
		git__free(key);
420
		return GIT_ENOTFOUND;
421
	}
422

423
	var = git_strmap_value_at(b->values, pos);
424

425 426
	result = regcomp(&preg, regexp, REG_EXTENDED);
	if (result < 0) {
427
		git__free(key);
428
		giterr_set_regex(&preg, result);
429
		regfree(&preg);
430
		return -1;
431
	}
432

433
	for (;;) {
434
		if (regexec(&preg, var->entry->value, 0, NULL, 0) == 0) {
435
			char *tmp = git__strdup(value);
436
			GITERR_CHECK_ALLOC(tmp);
437

438 439
			git__free((void *)var->entry->value);
			var->entry->value = tmp;
440 441 442
			replaced = 1;
		}

443
		if (var->next == NULL)
444
			break;
445 446 447

		var = var->next;
	}
448 449 450 451

	/* If we've reached the end of the variables and we haven't found it yet, we need to append it */
	if (!replaced) {
		newvar = git__malloc(sizeof(cvar_t));
452
		GITERR_CHECK_ALLOC(newvar);
453
		memset(newvar, 0x0, sizeof(cvar_t));
454 455 456 457 458 459
		newvar->entry = git__malloc(sizeof(git_config_entry));
		GITERR_CHECK_ALLOC(newvar->entry);
		memset(newvar->entry, 0x0, sizeof(git_config_entry));

		newvar->entry->name = git__strdup(var->entry->name);
		GITERR_CHECK_ALLOC(newvar->entry->name);
460

461 462
		newvar->entry->value = git__strdup(value);
		GITERR_CHECK_ALLOC(newvar->entry->value);
463

464
		newvar->entry->level = var->entry->level;
465 466 467 468

		var->next = newvar;
	}

469
	result = config_write(b, key, &preg, value);
470

471
	git__free(key);
472
	regfree(&preg);
473 474

	return result;
475 476
}

477 478
static int config_delete(git_config_file *cfg, const char *name)
{
479
	cvar_t *var;
480
	diskfile_backend *b = (diskfile_backend *)cfg;
481
	char *key;
482
	int result;
483
	khiter_t pos;
484

485 486
	if (normalize_name(name, &key) < 0)
		return -1;
487

488
	pos = git_strmap_lookup_index(b->values, key);
489
	git__free(key);
490

491 492
	if (!git_strmap_valid_index(b->values, pos)) {
		giterr_set(GITERR_CONFIG, "Could not find key '%s' to delete", name);
493
		return GIT_ENOTFOUND;
494
	}
495

496
	var = git_strmap_value_at(b->values, pos);
497

498 499 500 501
	if (var->next != NULL) {
		giterr_set(GITERR_CONFIG, "Cannot delete multivar with a single delete");
		return -1;
	}
502

503
	git_strmap_delete_at(b->values, pos);
504

505
	result = config_write(b, var->entry->name, NULL, NULL);
506

507 508
	cvar_free(var);
	return result;
509 510
}

511
int git_config_file__ondisk(git_config_file **out, const char *path)
512
{
513
	diskfile_backend *backend;
514

515
	backend = git__malloc(sizeof(diskfile_backend));
516
	GITERR_CHECK_ALLOC(backend);
517

518
	memset(backend, 0x0, sizeof(diskfile_backend));
519 520

	backend->file_path = git__strdup(path);
521
	GITERR_CHECK_ALLOC(backend->file_path);
522 523 524

	backend->parent.open = config_open;
	backend->parent.get = config_get;
525
	backend->parent.get_multivar = config_get_multivar;
526
	backend->parent.set = config_set;
527
	backend->parent.set_multivar = config_set_multivar;
528
	backend->parent.del = config_delete;
529 530 531
	backend->parent.foreach = file_foreach;
	backend->parent.free = backend_free;

532
	*out = (git_config_file *)backend;
533

534
	return 0;
535 536
}

537
static int cfg_getchar_raw(diskfile_backend *cfg)
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
{
	int c;

	c = *cfg->reader.read_ptr++;

	/*
	Win 32 line breaks: if we find a \r\n sequence,
	return only the \n as a newline
	*/
	if (c == '\r' && *cfg->reader.read_ptr == '\n') {
		cfg->reader.read_ptr++;
		c = '\n';
	}

	if (c == '\n')
		cfg->reader.line_number++;

	if (c == 0) {
		cfg->reader.eof = 1;
		c = '\n';
	}

	return c;
}

#define SKIP_WHITESPACE (1 << 1)
#define SKIP_COMMENTS (1 << 2)

566
static int cfg_getchar(diskfile_backend *cfg_file, int flags)
567 568 569 570 571 572 573 574
{
	const int skip_whitespace = (flags & SKIP_WHITESPACE);
	const int skip_comments = (flags & SKIP_COMMENTS);
	int c;

	assert(cfg_file->reader.read_ptr);

	do c = cfg_getchar_raw(cfg_file);
575
	while (skip_whitespace && git__isspace(c) &&
576
	       !cfg_file->reader.eof);
577 578 579 580 581 582 583 584 585 586 587 588

	if (skip_comments && (c == '#' || c == ';')) {
		do c = cfg_getchar_raw(cfg_file);
		while (c != '\n');
	}

	return c;
}

/*
 * Read the next char, but don't move the reading pointer.
 */
589
static int cfg_peek(diskfile_backend *cfg, int flags)
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
{
	void *old_read_ptr;
	int old_lineno, old_eof;
	int ret;

	assert(cfg->reader.read_ptr);

	old_read_ptr = cfg->reader.read_ptr;
	old_lineno = cfg->reader.line_number;
	old_eof = cfg->reader.eof;

	ret = cfg_getchar(cfg, flags);

	cfg->reader.read_ptr = old_read_ptr;
	cfg->reader.line_number = old_lineno;
	cfg->reader.eof = old_eof;

	return ret;
}

/*
 * Read and consume a line, returning it in newly-allocated memory.
 */
613
static char *cfg_readline(diskfile_backend *cfg, bool skip_whitespace)
614 615 616
{
	char *line = NULL;
	char *line_src, *line_end;
617
	size_t line_len;
618 619

	line_src = cfg->reader.read_ptr;
620

621 622
	if (skip_whitespace) {
		/* Skip empty empty lines */
623
		while (git__isspace(*line_src))
624 625
			++line_src;
	}
626

627
	line_end = strchr(line_src, '\n');
628

629
	/* no newline at EOF */
630 631 632
	if (line_end == NULL)
		line_end = strchr(line_src, 0);

633
	line_len = line_end - line_src;
634

635
	line = git__malloc(line_len + 1);
636 637 638
	if (line == NULL)
		return NULL;

639
	memcpy(line, line_src, line_len);
640

641
	do line[line_len] = '\0';
642
	while (line_len-- > 0 && git__isspace(line[line_len]));
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658

	if (*line_end == '\n')
		line_end++;

	if (*line_end == '\0')
		cfg->reader.eof = 1;

	cfg->reader.line_number++;
	cfg->reader.read_ptr = line_end;

	return line;
}

/*
 * Consume a line, without storing it anywhere
 */
659
static void cfg_consume_line(diskfile_backend *cfg)
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
{
	char *line_start, *line_end;

	line_start = cfg->reader.read_ptr;
	line_end = strchr(line_start, '\n');
	/* No newline at EOF */
	if(line_end == NULL){
		line_end = strchr(line_start, '\0');
	}

	if (*line_end == '\n')
		line_end++;

	if (*line_end == '\0')
		cfg->reader.eof = 1;

	cfg->reader.line_number++;
	cfg->reader.read_ptr = line_end;
}

680
GIT_INLINE(int) config_keychar(int c)
681 682 683 684
{
	return isalnum(c) || c == '-';
}

685
static int parse_section_header_ext(diskfile_backend *cfg, const char *line, const char *base_name, char **section_name)
686
{
687 688 689
	int c, rpos;
	char *first_quote, *last_quote;
	git_buf buf = GIT_BUF_INIT;
690 691 692 693 694 695 696 697 698 699
	int quote_marks;
	/*
	 * base_name is what came before the space. We should be at the
	 * first quotation mark, except for now, line isn't being kept in
	 * sync so we only really use it to calculate the length.
	 */

	first_quote = strchr(line, '"');
	last_quote = strrchr(line, '"');

700 701 702 703
	if (last_quote - first_quote == 0) {
		set_parse_error(cfg, 0, "Missing closing quotation mark in section header");
		return -1;
	}
704

705 706
	git_buf_grow(&buf, strlen(base_name) + last_quote - first_quote + 2);
	git_buf_printf(&buf, "%s.", base_name);
707 708 709 710 711 712 713 714 715 716 717 718

	rpos = 0;
	quote_marks = 0;

	line = first_quote;
	c = line[rpos++];

	/*
	 * At the end of each iteration, whatever is stored in c will be
	 * added to the string. In case of error, jump to out
	 */
	do {
719
		if (quote_marks == 2) {
720 721 722
			set_parse_error(cfg, rpos, "Unexpected text after closing quotes");
			git_buf_free(&buf);
			return -1;
723 724
		}

725 726
		switch (c) {
		case '"':
727
			++quote_marks;
728
			continue;
729

730 731
		case '\\':
			c = line[rpos++];
732

733 734 735 736
			switch (c) {
			case '"':
			case '\\':
				break;
737

738
			default:
739 740 741
				set_parse_error(cfg, rpos, "Unsupported escape sequence");
				git_buf_free(&buf);
				return -1;
742
			}
743

744 745 746 747
		default:
			break;
		}

748
		git_buf_putc(&buf, c);
749 750
	} while ((c = line[rpos++]) != ']');

751 752
	*section_name = git_buf_detach(&buf);
	return 0;
753 754
}

755
static int parse_section_header(diskfile_backend *cfg, char **section_out)
756 757 758
{
	char *name, *name_end;
	int name_length, c, pos;
759
	int result;
760 761
	char *line;

762
	line = cfg_readline(cfg, true);
763
	if (line == NULL)
764
		return -1;
765 766 767

	/* find the end of the variable's name */
	name_end = strchr(line, ']');
schu committed
768
	if (name_end == NULL) {
769
		git__free(line);
770 771
		set_parse_error(cfg, 0, "Missing ']' in section header");
		return -1;
schu committed
772
	}
773 774

	name = (char *)git__malloc((size_t)(name_end - line) + 1);
775
	GITERR_CHECK_ALLOC(name);
776 777 778 779 780 781

	name_length = 0;
	pos = 0;

	/* Make sure we were given a section header */
	c = line[pos++];
782
	assert(c == '[');
783 784 785 786

	c = line[pos++];

	do {
787
		if (git__isspace(c)){
788
			name[name_length] = '\0';
789
			result = parse_section_header_ext(cfg, line, name, section_out);
790 791
			git__free(line);
			git__free(name);
792
			return result;
793 794 795
		}

		if (!config_keychar(c) && c != '.') {
796 797
			set_parse_error(cfg, pos, "Unexpected character in header");
			goto fail_parse;
798 799
		}

800
		name[name_length++] = (char) tolower(c);
801 802 803

	} while ((c = line[pos++]) != ']');

schu committed
804
	if (line[pos - 1] != ']') {
805 806
		set_parse_error(cfg, pos, "Unexpected end of file");
		goto fail_parse;
schu committed
807
	}
808

809
	git__free(line);
810 811

	name[name_length] = 0;
812 813
	*section_out = name;

814 815 816
	return 0;

fail_parse:
817 818
	git__free(line);
	git__free(name);
819
	return -1;
820 821
}

822
static int skip_bom(diskfile_backend *cfg)
823
{
824
	static const char utf8_bom[] = { '\xef', '\xbb', '\xbf' };
825

826
	if (cfg->reader.buffer.size < sizeof(utf8_bom))
827
		return 0;
828

829 830 831
	if (memcmp(cfg->reader.read_ptr, utf8_bom, sizeof(utf8_bom)) == 0)
		cfg->reader.read_ptr += sizeof(utf8_bom);

Vicent Marti committed
832
	/* TODO: the reference implementation does pretty stupid
833 834 835
		shit with the BoM
	*/

836
	return 0;
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
}

/*
	(* basic types *)
	digit = "0".."9"
	integer = digit { digit }
	alphabet = "a".."z" + "A" .. "Z"

	section_char = alphabet | "." | "-"
	extension_char = (* any character except newline *)
	any_char = (* any character *)
	variable_char = "alphabet" | "-"


	(* actual grammar *)
	config = { section }

	section = header { definition }

	header = "[" section [subsection | subsection_ext] "]"

	subsection = "." section
	subsection_ext = "\"" extension "\""

	section = section_char { section_char }
	extension = extension_char { extension_char }

	definition = variable_name ["=" variable_value] "\n"

	variable_name = variable_char { variable_char }
	variable_value = string | boolean | integer

	string = quoted_string | plain_string
	quoted_string = "\"" plain_string "\""
	plain_string = { any_char }

	boolean = boolean_true | boolean_false
	boolean_true = "yes" | "1" | "true" | "on"
	boolean_false = "no" | "0" | "false" | "off"
*/

878
static int strip_comments(char *line, int in_quotes)
879
{
880
	int quote_count = in_quotes;
881 882 883 884 885 886 887 888 889 890 891 892
	char *ptr;

	for (ptr = line; *ptr; ++ptr) {
		if (ptr[0] == '"' && ptr > line && ptr[-1] != '\\')
			quote_count++;

		if ((ptr[0] == ';' || ptr[0] == '#') && (quote_count % 2) == 0) {
			ptr[0] = '\0';
			break;
		}
	}

893
	/* skip any space at the end */
894
	if (git__isspace(ptr[-1])) {
895
		ptr--;
896
	}
897 898 899
	ptr[0] = '\0';

	return quote_count;
900 901
}

902
static int config_parse(diskfile_backend *cfg_file, unsigned int level)
903
{
904
	int c;
905 906 907
	char *current_section = NULL;
	char *var_name;
	char *var_value;
908
	cvar_t *var, *existing;
909
	git_buf buf = GIT_BUF_INIT;
910
	int result = 0;
911
	khiter_t pos;
912

913
	/* Initialize the reading position */
914
	cfg_file->reader.read_ptr = cfg_file->reader.buffer.ptr;
915 916
	cfg_file->reader.eof = 0;

917 918
	/* If the file is empty, there's nothing for us to do */
	if (*cfg_file->reader.read_ptr == '\0')
919
		return 0;
920

921 922
	skip_bom(cfg_file);

923
	while (result == 0 && !cfg_file->reader.eof) {
924 925 926 927

		c = cfg_peek(cfg_file, SKIP_WHITESPACE);

		switch (c) {
928 929
		case '\n': /* EOF when peeking, set EOF in the reader to exit the loop */
			cfg_file->reader.eof = 1;
930 931 932
			break;

		case '[': /* section header, new section begins */
933
			git__free(current_section);
934
			current_section = NULL;
935
			result = parse_section_header(cfg_file, &current_section);
936 937 938 939 940 941 942 943
			break;

		case ';':
		case '#':
			cfg_consume_line(cfg_file);
			break;

		default: /* assume variable declaration */
944 945
			result = parse_variable(cfg_file, &var_name, &var_value);
			if (result < 0)
946 947
				break;

948
			var = git__malloc(sizeof(cvar_t));
949
			GITERR_CHECK_ALLOC(var);
950
			memset(var, 0x0, sizeof(cvar_t));
951 952 953
			var->entry = git__malloc(sizeof(git_config_entry));
			GITERR_CHECK_ALLOC(var->entry);
			memset(var->entry, 0x0, sizeof(git_config_entry));
954

955 956 957 958
			git__strtolower(var_name);
			git_buf_printf(&buf, "%s.%s", current_section, var_name);
			git__free(var_name);

959 960
			if (git_buf_oom(&buf))
				return -1;
961

962 963 964
			var->entry->name = git_buf_detach(&buf);
			var->entry->value = var_value;
			var->entry->level = level;
965

966
			/* Add or append the new config option */
967
			pos = git_strmap_lookup_index(cfg_file->values, var->entry->name);
968
			if (!git_strmap_valid_index(cfg_file->values, pos)) {
969
				git_strmap_insert(cfg_file->values, var->entry->name, var, result);
970 971 972
				if (result < 0)
					break;
				result = 0;
973
			} else {
974
				existing = git_strmap_value_at(cfg_file->values, pos);
975 976 977 978 979
				while (existing->next != NULL) {
					existing = existing->next;
				}
				existing->next = var;
			}
980 981 982 983 984

			break;
		}
	}

985
	git__free(current_section);
986
	return result;
987 988
}

989
static int write_section(git_filebuf *file, const char *key)
990
{
991
	int result;
992
	const char *dot;
993
	git_buf buf = GIT_BUF_INIT;
994

995
	/* All of this just for [section "subsection"] */
996
	dot = strchr(key, '.');
997
	git_buf_putc(&buf, '[');
998
	if (dot == NULL) {
999
		git_buf_puts(&buf, key);
1000
	} else {
1001
		char *escaped;
1002
		git_buf_put(&buf, key, dot - key);
1003 1004 1005 1006
		escaped = escape_value(dot + 1);
		GITERR_CHECK_ALLOC(escaped);
		git_buf_printf(&buf, " \"%s\"", escaped);
		git__free(escaped);
1007 1008
	}
	git_buf_puts(&buf, "]\n");
1009

1010
	if (git_buf_oom(&buf))
1011
		return -1;
1012

1013
	result = git_filebuf_write(file, git_buf_cstr(&buf), buf.size);
1014
	git_buf_free(&buf);
1015

1016
	return result;
1017 1018 1019 1020 1021
}

/*
 * This is pretty much the parsing, except we write out anything we don't have
 */
1022
static int config_write(diskfile_backend *cfg, const char *key, const regex_t *preg, const char* value)
1023
{
1024 1025 1026
	int result, c;
	int section_matches = 0, last_section_matched = 0, preg_replaced = 0, write_trailer = 0;
	const char *pre_end = NULL, *post_start = NULL, *data_start;
1027
	char *current_section = NULL, *section, *name, *ldot;
1028
	git_filebuf file = GIT_FILEBUF_INIT;
1029 1030

	/* We need to read in our own config file */
1031
	result = git_futils_readbuffer(&cfg->reader.buffer, cfg->file_path);
1032 1033

	/* Initialise the reading position */
1034
	if (result == GIT_ENOTFOUND) {
1035 1036 1037
		cfg->reader.read_ptr = NULL;
		cfg->reader.eof = 1;
		data_start = NULL;
1038
		git_buf_clear(&cfg->reader.buffer);
1039
	} else if (result == 0) {
1040
		cfg->reader.read_ptr = cfg->reader.buffer.ptr;
1041 1042
		cfg->reader.eof = 0;
		data_start = cfg->reader.read_ptr;
1043 1044
	} else {
		return -1; /* OS error when reading the file */
1045
	}
1046 1047

	/* Lock the file */
1048 1049
	if (git_filebuf_open(&file, cfg->file_path, 0) < 0)
		return -1;
1050 1051

	skip_bom(cfg);
1052 1053 1054
	ldot = strrchr(key, '.');
	name = ldot + 1;
	section = git__strndup(key, ldot - key);
1055

1056
	while (!cfg->reader.eof) {
1057 1058
		c = cfg_peek(cfg, SKIP_WHITESPACE);

1059
		if (c == '\0') { /* We've arrived at the end of the file */
1060 1061
			break;

1062
		} else if (c == '[') { /* section header, new section begins */
1063 1064 1065 1066 1067 1068 1069 1070
			/*
			 * We set both positions to the current one in case we
			 * need to add a variable to the end of a section. In that
			 * case, we want both variables to point just before the
			 * new section. If we actually want to replace it, the
			 * default case will take care of updating them.
			 */
			pre_end = post_start = cfg->reader.read_ptr;
1071 1072

			git__free(current_section);
1073
			current_section = NULL;
1074 1075
			if (parse_section_header(cfg, &current_section) < 0)
				goto rewrite_fail;
1076 1077 1078

			/* Keep track of when it stops matching */
			last_section_matched = section_matches;
1079
			section_matches = !strcmp(current_section, section);
1080
		}
1081

1082
		else if (c == ';' || c == '#') {
1083
			cfg_consume_line(cfg);
1084
		}
1085

1086
		else {
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
			/*
			 * If the section doesn't match, but the last section did,
			 * it means we need to add a variable (so skip the line
			 * otherwise). If both the section and name match, we need
			 * to overwrite the variable (so skip the line
			 * otherwise). pre_end needs to be updated each time so we
			 * don't loose that information, but we only need to
			 * update post_start if we're going to use it in this
			 * iteration.
			 */
			if (!section_matches) {
				if (!last_section_matched) {
					cfg_consume_line(cfg);
1100
					continue;
1101 1102
				}
			} else {
1103 1104
				int has_matched = 0;
				char *var_name, *var_value;
1105

1106
				pre_end = cfg->reader.read_ptr;
1107 1108
				if (parse_variable(cfg, &var_name, &var_value) < 0)
					goto rewrite_fail;
1109

1110 1111 1112
				/* First try to match the name of the variable */
				if (strcasecmp(name, var_name) == 0)
					has_matched = 1;
1113

1114 1115 1116 1117
				/* If the name matches, and we have a regex to match the
				 * value, try to match it */
				if (has_matched && preg != NULL)
					has_matched = (regexec(preg, var_value, 0, NULL, 0) == 0);
1118

1119 1120
				git__free(var_name);
				git__free(var_value);
1121

1122 1123 1124
				/* if there is no match, keep going */
				if (!has_matched)
					continue;
1125

1126 1127 1128
				post_start = cfg->reader.read_ptr;
			}

1129 1130 1131
			/* We've found the variable we wanted to change, so
			 * write anything up to it */
			git_filebuf_write(&file, data_start, pre_end - data_start);
1132
			preg_replaced = 1;
1133

1134 1135 1136 1137
			/* Then replace the variable. If the value is NULL, it
			 * means we want to delete it, so don't write anything. */
			if (value != NULL) {
				git_filebuf_printf(&file, "\t%s = %s\n", name, value);
1138 1139
			}

1140
			/* multiline variable? we need to keep reading lines to match */
1141 1142 1143 1144 1145
			if (preg != NULL) {
				data_start = post_start;
				continue;
			}

1146 1147
			write_trailer = 1;
			break; /* break from the loop */
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
		}
	}

	/*
	 * Being here can mean that
	 *
	 * 1) our section is the last one in the file and we're
	 * adding a variable
	 *
	 * 2) we didn't find a section for us so we need to create it
	 * ourselves.
	 *
1160 1161 1162 1163 1164 1165
	 * 3) we're setting a multivar with a regex, which means we
	 * continue to search for matching values
	 *
	 * In the last case, if we've already replaced a value, we
	 * want to write the rest of the file. Otherwise we need to write
	 * out the whole file and then the new variable.
1166
	 */
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
	if (write_trailer) {
		/* Write out rest of the file */
		git_filebuf_write(&file, post_start, cfg->reader.buffer.size - (post_start - data_start));
	} else {
		if (preg_replaced) {
			git_filebuf_printf(&file, "\n%s", data_start);
		} else {
			git_filebuf_write(&file, cfg->reader.buffer.ptr, cfg->reader.buffer.size);

			/* And now if we just need to add a variable */
			if (!section_matches && write_section(&file, section) < 0)
				goto rewrite_fail;

			/* Sanity check: if we are here, and value is NULL, that means that somebody
			 * touched the config file after our intial read. We should probably assert()
			 * this, but instead we'll handle it gracefully with an error. */
			if (value == NULL) {
				giterr_set(GITERR_CONFIG,
					"Race condition when writing a config file (a cvar has been removed)");
				goto rewrite_fail;
			}
1188

1189 1190 1191 1192
			/* If we are here, there is at least a section line */
			if (*(cfg->reader.buffer.ptr + cfg->reader.buffer.size - 1) != '\n')
				git_filebuf_write(&file, "\n", 1);

1193 1194
			git_filebuf_printf(&file, "\t%s = %s\n", name, value);
		}
1195 1196
	}

1197 1198
	git__free(section);
	git__free(current_section);
1199

1200 1201 1202
	result = git_filebuf_commit(&file, GIT_CONFIG_FILE_MODE);
	git_buf_free(&cfg->reader.buffer);
	return result;
1203

1204
rewrite_fail:
1205
	git__free(section);
1206
	git__free(current_section);
1207

1208
	git_filebuf_cleanup(&file);
1209
	git_buf_free(&cfg->reader.buffer);
1210
	return -1;
1211 1212
}

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
static const char *escapes = "ntb\"\\";
static const char *escaped = "\n\t\b\"\\";

/* Escape the values to write them to the file */
static char *escape_value(const char *ptr)
{
	git_buf buf = GIT_BUF_INIT;
	size_t len;
	const char *esc;

	assert(ptr);

	len = strlen(ptr);
	git_buf_grow(&buf, len);

	while (*ptr != '\0') {
		if ((esc = strchr(escaped, *ptr)) != NULL) {
			git_buf_putc(&buf, '\\');
			git_buf_putc(&buf, escapes[esc - escaped]);
		} else {
			git_buf_putc(&buf, *ptr);
		}
		ptr++;
	}

	if (git_buf_oom(&buf)) {
		git_buf_free(&buf);
		return NULL;
	}

	return git_buf_detach(&buf);
}

1246 1247
/* '\"' -> '"' etc */
static char *fixup_line(const char *ptr, int quote_count)
1248
{
1249 1250 1251 1252 1253
	char *str = git__malloc(strlen(ptr) + 1);
	char *out = str, *esc;

	if (str == NULL)
		return NULL;
1254

1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
	while (*ptr != '\0') {
		if (*ptr == '"') {
			quote_count++;
		} else if (*ptr != '\\') {
			*out++ = *ptr;
		} else {
			/* backslash, check the next char */
			ptr++;
			/* if we're at the end, it's a multiline, so keep the backslash */
			if (*ptr == '\0') {
				*out++ = '\\';
				goto out;
			}
			if ((esc = strchr(escapes, *ptr)) != NULL) {
				*out++ = escaped[esc - escapes];
			} else {
				git__free(str);
				giterr_set(GITERR_CONFIG, "Invalid escape at %s", ptr);
				return NULL;
			}
		}
		ptr++;
	}

out:
	*out = '\0';

	return str;
}
1284 1285 1286

static int is_multiline_var(const char *str)
{
1287 1288
	const char *end = str + strlen(str);
	return (end > str) && (end[-1] == '\\');
1289 1290
}

1291
static int parse_multiline_variable(diskfile_backend *cfg, git_buf *value, int in_quotes)
1292
{
1293 1294
	char *line = NULL, *proc_line = NULL;
	int quote_count;
1295 1296

	/* Check that the next line exists */
1297
	line = cfg_readline(cfg, false);
1298
	if (line == NULL)
1299
		return -1;
1300 1301 1302

	/* We've reached the end of the file, there is input missing */
	if (line[0] == '\0') {
1303 1304 1305
		set_parse_error(cfg, 0, "Unexpected end of file while parsing multine var");
		git__free(line);
		return -1;
1306 1307
	}

1308
	quote_count = strip_comments(line, !!in_quotes);
1309 1310 1311

	/* If it was just a comment, pretend it didn't exist */
	if (line[0] == '\0') {
1312
		git__free(line);
1313
		return parse_multiline_variable(cfg, value, quote_count);
1314
		/* TODO: unbounded recursion. This **could** be exploitable */
1315 1316
	}

1317 1318 1319 1320
	/* Drop the continuation character '\': to closely follow the UNIX
	 * standard, this character **has** to be last one in the buf, with
	 * no whitespace after it */
	assert(is_multiline_var(value->ptr));
nulltoken committed
1321
	git_buf_truncate(value, git_buf_len(value) - 1);
1322

1323 1324 1325 1326
	proc_line = fixup_line(line, in_quotes);
	if (proc_line == NULL) {
		git__free(line);
		return -1;
1327
	}
1328
	/* add this line to the multiline var */
1329
	git_buf_puts(value, proc_line);
1330
	git__free(line);
1331
	git__free(proc_line);
1332 1333

	/*
1334 1335
	 * If we need to continue reading the next line, let's just
	 * keep putting stuff in the buffer
1336
	 */
1337
	if (is_multiline_var(value->ptr))
1338
		return parse_multiline_variable(cfg, value, quote_count);
1339

1340
	return 0;
1341 1342
}

1343
static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_value)
1344 1345 1346 1347
{
	const char *var_end = NULL;
	const char *value_start = NULL;
	char *line;
1348
	int quote_count;
1349

1350
	line = cfg_readline(cfg, true);
1351
	if (line == NULL)
1352
		return -1;
1353

1354
	quote_count = strip_comments(line, 0);
1355 1356 1357 1358 1359 1360 1361 1362

	var_end = strchr(line, '=');

	if (var_end == NULL)
		var_end = strchr(line, '\0');
	else
		value_start = var_end + 1;

Russell Belfer committed
1363 1364
	do var_end--;
	while (git__isspace(*var_end));
1365

1366 1367
	*var_name = git__strndup(line, var_end - line + 1);
	GITERR_CHECK_ALLOC(*var_name);
1368

1369 1370
	/* If there is no value, boolean true is assumed */
	*var_value = NULL;
1371 1372 1373 1374 1375

	/*
	 * Now, let's try to parse the value
	 */
	if (value_start != NULL) {
1376
		while (git__isspace(value_start[0]))
1377 1378 1379
			value_start++;

		if (is_multiline_var(value_start)) {
1380
			git_buf multi_value = GIT_BUF_INIT;
1381 1382 1383
			char *proc_line = fixup_line(value_start, 0);
			GITERR_CHECK_ALLOC(proc_line);
			git_buf_puts(&multi_value, proc_line);
1384
			git__free(proc_line);
1385
			if (parse_multiline_variable(cfg, &multi_value, quote_count) < 0 || git_buf_oom(&multi_value)) {
1386
				git__free(*var_name);
1387 1388 1389
				git__free(line);
				git_buf_free(&multi_value);
				return -1;
1390
			}
1391

1392
			*var_value = git_buf_detach(&multi_value);
1393

1394 1395
		}
		else if (value_start[0] != '\0') {
1396
			*var_value = fixup_line(value_start, 0);
1397
			GITERR_CHECK_ALLOC(*var_value);
1398 1399 1400 1401
		}

	}

1402
	git__free(line);
1403
	return 0;
1404
}