config_parse.c 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "config_parse.h"

#include <ctype.h>

12 13 14
const char *git_config_escapes = "ntb\"\\";
const char *git_config_escaped = "\n\t\b\"\\";

15 16
static void set_parse_error(git_config_parser *reader, int col, const char *error_str)
{
17 18 19
	if (col)
		git_error_set(GIT_ERROR_CONFIG,
		              "failed to parse config file: %s (in %s:%"PRIuZ", column %d)",
20
		              error_str, reader->path, reader->ctx.line_num, col);
21 22 23
	else
		git_error_set(GIT_ERROR_CONFIG,
		              "failed to parse config file: %s (in %s:%"PRIuZ")",
24
		              error_str, reader->path, reader->ctx.line_num);
25 26 27 28 29 30 31 32 33 34 35 36 37 38
}


GIT_INLINE(int) config_keychar(int c)
{
	return isalnum(c) || c == '-';
}

static int strip_comments(char *line, int in_quotes)
{
	int quote_count = in_quotes, backslash_count = 0;
	char *ptr;

	for (ptr = line; *ptr; ++ptr) {
39
		if (ptr[0] == '"' && ((ptr > line && ptr[-1] != '\\') || ptr == line))
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
			quote_count++;

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

		if (ptr[0] == '\\')
			backslash_count++;
		else
			backslash_count = 0;
	}

	/* skip any space at the end */
	while (ptr > line && git__isspace(ptr[-1])) {
		ptr--;
	}
	ptr[0] = '\0';

	return quote_count;
}


65
static int parse_subsection_header(git_config_parser *reader, const char *line, size_t pos, const char *base_name, char **section_name)
66 67
{
	int c, rpos;
68
	const char *first_quote, *last_quote;
69
	const char *line_start = line;
70
	git_str buf = GIT_STR_INIT;
71 72
	size_t quoted_len, alloc_len, base_name_len = strlen(base_name);

73 74 75
	/* Skip any additional whitespace before our section name */
	while (git__isspace(line[pos]))
		pos++;
76

77 78
	/* We should be at the first quotation mark. */
	if (line[pos] != '"') {
79
		set_parse_error(reader, 0, "missing quotation marks in section header");
80 81 82
		goto end_error;
	}

83
	first_quote = &line[pos];
84 85 86
	last_quote = strrchr(line, '"');
	quoted_len = last_quote - first_quote;

87 88 89 90 91
	if ((last_quote - line) > INT_MAX) {
		set_parse_error(reader, 0, "invalid section header, line too long");
		goto end_error;
	}

92
	if (quoted_len == 0) {
93
		set_parse_error(reader, 0, "missing closing quotation mark in section header");
94 95 96
		goto end_error;
	}

97 98
	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, base_name_len, quoted_len);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
99

100 101
	if (git_str_grow(&buf, alloc_len) < 0 ||
	    git_str_printf(&buf, "%s.", base_name) < 0)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
		goto end_error;

	rpos = 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 {

		switch (c) {
		case 0:
117
			set_parse_error(reader, 0, "unexpected end-of-line in section header");
118 119 120 121 122 123 124 125 126
			goto end_error;

		case '"':
			goto end_parse;

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

			if (c == 0) {
127
				set_parse_error(reader, rpos, "unexpected end-of-line in section header");
128 129 130 131 132 133 134
				goto end_error;
			}

		default:
			break;
		}

135
		git_str_putc(&buf, (char)c);
136 137 138 139
		c = line[++rpos];
	} while (line + rpos < last_quote);

end_parse:
140
	if (git_str_oom(&buf))
141 142 143
		goto end_error;

	if (line[rpos] != '"' || line[rpos + 1] != ']') {
144
		set_parse_error(reader, rpos, "unexpected text after closing quotes");
145
		git_str_dispose(&buf);
146 147 148
		return -1;
	}

149
	*section_name = git_str_detach(&buf);
150
	return (int)(&line[rpos + 2] - line_start); /* rpos is at the closing quote */
151 152

end_error:
153
	git_str_dispose(&buf);
154 155 156 157 158 159 160 161 162 163 164 165

	return -1;
}

static int parse_section_header(git_config_parser *reader, char **section_out)
{
	char *name, *name_end;
	int name_length, c, pos;
	int result;
	char *line;
	size_t line_len;

166 167
	git_parse_advance_ws(&reader->ctx);
	line = git__strndup(reader->ctx.line, reader->ctx.line_len);
168 169 170 171 172 173 174
	if (line == NULL)
		return -1;

	/* find the end of the variable's name */
	name_end = strrchr(line, ']');
	if (name_end == NULL) {
		git__free(line);
175
		set_parse_error(reader, 0, "missing ']' in section header");
176 177 178
		return -1;
	}

179
	GIT_ERROR_CHECK_ALLOC_ADD(&line_len, (size_t)(name_end - line), 1);
180
	name = git__malloc(line_len);
181
	GIT_ERROR_CHECK_ALLOC(name);
182 183 184 185 186 187

	name_length = 0;
	pos = 0;

	/* Make sure we were given a section header */
	c = line[pos++];
188
	GIT_ASSERT(c == '[');
189 190 191 192 193 194

	c = line[pos++];

	do {
		if (git__isspace(c)){
			name[name_length] = '\0';
195
			result = parse_subsection_header(reader, line, pos, name, section_out);
196 197 198 199 200 201
			git__free(line);
			git__free(name);
			return result;
		}

		if (!config_keychar(c) && c != '.') {
202
			set_parse_error(reader, pos, "unexpected character in header");
203 204 205 206 207 208 209 210
			goto fail_parse;
		}

		name[name_length++] = (char)git__tolower(c);

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

	if (line[pos - 1] != ']') {
211
		set_parse_error(reader, pos, "unexpected end of file");
212 213 214 215 216 217 218 219
		goto fail_parse;
	}

	git__free(line);

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

220
	return pos;
221 222 223 224 225 226 227

fail_parse:
	git__free(line);
	git__free(name);
	return -1;
}

228
static int skip_bom(git_parse_ctx *parser)
229
{
230 231 232
	git_str buf = GIT_STR_INIT_CONST(parser->content, parser->content_len);
	git_str_bom_t bom;
	int bom_offset = git_str_detect_bom(&bom, &buf);
233

234
	if (bom == GIT_STR_BOM_UTF8)
235
		git_parse_advance_chars(parser, bom_offset);
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

	/* TODO: reference implementation is pretty stupid with BoM */

	return 0;
}

/*
	(* 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"
*/

/* '\"' -> '"' etc */
static int unescape_line(
	char **out, bool *is_multi, const char *ptr, int quote_count)
{
	char *str, *fixed, *esc;
	size_t ptr_len = strlen(ptr), alloc_len;

	*is_multi = false;

	if (GIT_ADD_SIZET_OVERFLOW(&alloc_len, ptr_len, 1) ||
		(str = git__malloc(alloc_len)) == NULL) {
		return -1;
	}

	fixed = str;

	while (*ptr != '\0') {
		if (*ptr == '"') {
			quote_count++;
		} else if (*ptr != '\\') {
			*fixed++ = *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') {
				*is_multi = true;
				goto done;
			}
			if ((esc = strchr(git_config_escapes, *ptr)) != NULL) {
				*fixed++ = git_config_escaped[esc - git_config_escapes];
			} else {
				git__free(str);
314
				git_error_set(GIT_ERROR_CONFIG, "invalid escape at %s", ptr);
315 316 317 318 319 320 321 322 323 324 325 326 327
				return -1;
			}
		}
		ptr++;
	}

done:
	*fixed = '\0';
	*out = str;

	return 0;
}

328
static int parse_multiline_variable(git_config_parser *reader, git_str *value, int in_quotes, size_t *line_len)
329 330
{
	int quote_count;
331 332 333
	bool multiline = true;

	while (multiline) {
334 335 336
		char *line = NULL, *proc_line = NULL;
		int error;

337 338 339
		/* Check that the next line exists */
		git_parse_advance_line(&reader->ctx);
		line = git__strndup(reader->ctx.line, reader->ctx.line_len);
340
		GIT_ERROR_CHECK_ALLOC(line);
341 342 343 344
		if (GIT_ADD_SIZET_OVERFLOW(line_len, *line_len, reader->ctx.line_len)) {
			error = -1;
			goto out;
		}
345

346 347
		/*
		 * We've reached the end of the file, there is no continuation.
348 349 350
		 * (this is not an error).
		 */
		if (line[0] == '\0') {
351 352
			error = 0;
			goto out;
353
		}
354

355
		/* If it was just a comment, pretend it didn't exist */
356
		quote_count = strip_comments(line, in_quotes);
357 358
		if (line[0] == '\0')
			goto next;
359

360 361 362
		if ((error = unescape_line(&proc_line, &multiline,
					   line, in_quotes)) < 0)
			goto out;
363

364
		/* Add this line to the multiline var */
365
		if ((error = git_str_puts(value, proc_line)) < 0)
366
			goto out;
367

368
next:
369
		git__free(line);
370 371
		git__free(proc_line);
		in_quotes = quote_count;
372 373 374 375 376 377
		continue;

out:
		git__free(line);
		git__free(proc_line);
		return error;
378
	}
379

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	return 0;
}

GIT_INLINE(bool) is_namechar(char c)
{
	return isalnum(c) || c == '-';
}

static int parse_name(
	char **name, const char **value, git_config_parser *reader, const char *line)
{
	const char *name_end = line, *value_start;

	*name = NULL;
	*value = NULL;

	while (*name_end && is_namechar(*name_end))
		name_end++;

	if (line == name_end) {
400
		set_parse_error(reader, 0, "invalid configuration key");
401 402 403 404 405 406 407 408 409 410 411
		return -1;
	}

	value_start = name_end;

	while (*value_start && git__isspace(*value_start))
		value_start++;

	if (*value_start == '=') {
		*value = value_start + 1;
	} else if (*value_start) {
412
		set_parse_error(reader, 0, "invalid configuration key");
413 414 415 416 417 418 419 420 421
		return -1;
	}

	if ((*name = git__strndup(line, name_end - line)) == NULL)
		return -1;

	return 0;
}

422
static int parse_variable(git_config_parser *reader, char **var_name, char **var_value, size_t *line_len)
423 424
{
	const char *value_start = NULL;
425 426
	char *line = NULL, *name = NULL, *value = NULL;
	int quote_count, error;
427 428
	bool multiline;

429 430 431
	*var_name = NULL;
	*var_value = NULL;

432 433
	git_parse_advance_ws(&reader->ctx);
	line = git__strndup(reader->ctx.line, reader->ctx.line_len);
434
	GIT_ERROR_CHECK_ALLOC(line);
435 436 437

	quote_count = strip_comments(line, 0);

438 439
	if ((error = parse_name(&name, &value_start, reader, line)) < 0)
		goto out;
440 441 442 443 444 445 446 447

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

448 449
		if ((error = unescape_line(&value, &multiline, value_start, 0)) < 0)
			goto out;
450 451

		if (multiline) {
452 453
			git_str multi_value = GIT_STR_INIT;
			git_str_attach(&multi_value, value, 0);
454
			value = NULL;
455

456
			if (parse_multiline_variable(reader, &multi_value, quote_count % 2, line_len) < 0 ||
457
			    git_str_oom(&multi_value)) {
458
				error = -1;
459
				git_str_dispose(&multi_value);
460
				goto out;
461 462
			}

463
			value = git_str_detach(&multi_value);
464 465 466
		}
	}

467 468 469 470
	*var_name = name;
	*var_value = value;
	name = NULL;
	value = NULL;
471

472 473 474
out:
	git__free(name);
	git__free(value);
475
	git__free(line);
476
	return error;
477 478
}

479 480 481 482 483 484 485 486 487 488 489
int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen)
{
	out->path = path;
	return git_parse_ctx_init(&out->ctx, data, datalen);
}

void git_config_parser_dispose(git_config_parser *parser)
{
	git_parse_ctx_clear(&parser->ctx);
}

490 491 492 493 494 495
int git_config_parse(
	git_config_parser *parser,
	git_config_parser_section_cb on_section,
	git_config_parser_variable_cb on_variable,
	git_config_parser_comment_cb on_comment,
	git_config_parser_eof_cb on_eof,
496
	void *payload)
497
{
498
	git_parse_ctx *ctx;
499
	char *current_section = NULL, *var_name = NULL, *var_value = NULL;
500 501
	int result = 0;

502
	ctx = &parser->ctx;
503

504
	skip_bom(ctx);
505

506
	for (; ctx->remain_len > 0; git_parse_advance_line(ctx)) {
507 508
		const char *line_start;
		size_t line_len;
509
		char c;
510

511 512 513 514
	restart:
		line_start = ctx->line;
		line_len = ctx->line_len;

515 516 517 518 519
		/*
		 * Get either first non-whitespace character or, if that does
		 * not exist, the first whitespace character. This is required
		 * to preserve whitespaces when writing back the file.
		 */
520 521 522
		if (git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE) < 0 &&
		    git_parse_peek(&c, ctx, 0) < 0)
			continue;
523

524
		switch (c) {
525 526 527 528
		case '[': /* section header, new section begins */
			git__free(current_section);
			current_section = NULL;

529 530 531 532 533 534 535
			result = parse_section_header(parser, &current_section);
			if (result < 0)
				break;

			git_parse_advance_chars(ctx, result);

			if (on_section)
536
				result = on_section(parser, current_section, line_start, line_len, payload);
537 538 539 540 541 542 543 544 545 546
			/*
			 * After we've parsed the section header we may not be
			 * done with the line. If there's still data in there,
			 * run the next loop with the rest of the current line
			 * instead of moving forward.
			 */

			if (!git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE))
				goto restart;

547 548 549
			break;

		case '\n': /* comment or whitespace-only */
550
		case '\r':
551 552
		case ' ':
		case '\t':
553 554 555
		case ';':
		case '#':
			if (on_comment) {
556
				result = on_comment(parser, line_start, line_len, payload);
557 558 559 560
			}
			break;

		default: /* assume variable declaration */
561
			if ((result = parse_variable(parser, &var_name, &var_value, &line_len)) == 0 && on_variable) {
562
				result = on_variable(parser, current_section, var_name, var_value, line_start, line_len, payload);
563 564
				git__free(var_name);
				git__free(var_value);
565
			}
566

567 568
			break;
		}
569 570 571

		if (result < 0)
			goto out;
572 573 574
	}

	if (on_eof)
575
		result = on_eof(parser, current_section, payload);
576

577
out:
578 579 580
	git__free(current_section);
	return result;
}