Line Flow Count Block(s) Source
1 - /*
2 - * Copyright (C) the libgit2 contributors. All rights reserved.
3 - *
4 - * This file is part of libgit2, distributed under the GNU GPL v2 with
5 - * a Linking Exception. For full terms see the included COPYING file.
6 - */
7 -
8 - #include "config_parse.h"
9 -
10 - #include "buf_text.h"
11 -
12 - #include <ctype.h>
13 -
14 - const char *git_config_escapes = "ntb\"\\";
15 - const char *git_config_escaped = "\n\t\b\"\\";
16 -
17 16 2 static void set_parse_error(git_config_parser *reader, int col, const char *error_str)
18 - {
19 16 2 if (col)
20 5 3 git_error_set(GIT_ERROR_CONFIG,
21 - "failed to parse config file: %s (in %s:%"PRIuZ", column %d)",
22 - error_str, reader->path, reader->ctx.line_num, col);
23 - else
24 11 4 git_error_set(GIT_ERROR_CONFIG,
25 - "failed to parse config file: %s (in %s:%"PRIuZ")",
26 - error_str, reader->path, reader->ctx.line_num);
27 16 5 }
28 -
29 -
30 345273 2 GIT_INLINE(int) config_keychar(int c)
31 - {
32 345273 2 return isalnum(c) || c == '-';
33 - }
34 -
35 - 2 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files)static int strip_comments(char *line, int in_quotes)
36 - {
37 - 2 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) int quote_count = in_quotes, backslash_count = 0;
38 - char *ptr;
39 -
40 - 2,15,16 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) for (ptr = line; *ptr; ++ptr) {
41 - 3-5 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) if (ptr[0] == '"' && ptr > line && ptr[-1] != '\\')
42 - 6 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) quote_count++;
43 -
44 - 7-9 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) if ((ptr[0] == ';' || ptr[0] == '#') &&
45 - 9,10 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) (quote_count % 2) == 0 &&
46 - 10 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) (backslash_count % 2) == 0) {
47 - 11 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) ptr[0] = '\0';
48 - 11 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) break;
49 - }
50 -
51 - 12 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) if (ptr[0] == '\\')
52 - 13 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) backslash_count++;
53 - else
54 - 14 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) backslash_count = 0;
55 - }
56 -
57 - /* skip any space at the end */
58 - 17,19-21 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) while (ptr > line && git__isspace(ptr[-1])) {
59 - 18 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) ptr--;
60 - }
61 - 22 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) ptr[0] = '\0';
62 -
63 - 22 suppressed: function cannot be solved strip_comments (automatic due to inconsistent arc counts in .gcda files) return quote_count;
64 - }
65 -
66 -
67 30829 2 static int parse_subsection_header(git_config_parser *reader, const char *line, size_t pos, const char *base_name, char **section_name)
68 - {
69 - int c, rpos;
70 - const char *first_quote, *last_quote;
71 30829 2 const char *line_start = line;
72 30829 2 git_buf buf = GIT_BUF_INIT;
73 30829 2 size_t quoted_len, alloc_len, base_name_len = strlen(base_name);
74 -
75 - /* Skip any additional whitespace before our section name */
76 30831 2,4,5 while (git__isspace(line[pos]))
77 2 3 pos++;
78 -
79 - /* We should be at the first quotation mark. */
80 30829 6 if (line[pos] != '"') {
81 4 7 set_parse_error(reader, 0, "missing quotation marks in section header");
82 4 53 goto end_error;
83 - }
84 -
85 30825 8 first_quote = &line[pos];
86 30825 8 last_quote = strrchr(line, '"');
87 30825 8 quoted_len = last_quote - first_quote;
88 -
89 30825 8 if ((last_quote - line) > INT_MAX) {
90 ##### 9 set_parse_error(reader, 0, "invalid section header, line too long");
91 ##### 10 goto end_error;
92 - }
93 -
94 30825 11 if (quoted_len == 0) {
95 ##### 12 set_parse_error(reader, 0, "missing closing quotation mark in section header");
96 ##### 13 goto end_error;
97 - }
98 -
99 30825 14-20 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, base_name_len, quoted_len);
100 30825 21-27 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
101 -
102 30825 28,29,31 if (git_buf_grow(&buf, alloc_len) < 0 ||
103 30825 30 git_buf_printf(&buf, "%s.", base_name) < 0)
104 - goto end_error;
105 -
106 30825 32 rpos = 0;
107 -
108 30825 32 line = first_quote;
109 30825 32 c = line[++rpos];
110 -
111 - /*
112 - * At the end of each iteration, whatever is stored in c will be
113 - * added to the string. In case of error, jump to out
114 - */
115 - do {
116 -
117 368371 33 switch (c) {
118 - case 0:
119 ##### 34 set_parse_error(reader, 0, "unexpected end-of-line in section header");
120 ##### 35 goto end_error;
121 -
122 - case '"':
123 58 36 goto end_parse;
124 -
125 - case '\\':
126 14 37 c = line[++rpos];
127 -
128 14 37 if (c == 0) {
129 ##### 38 set_parse_error(reader, rpos, "unexpected end-of-line in section header");
130 ##### 39 goto end_error;
131 - }
132 -
133 - default:
134 368313 40 break;
135 - }
136 -
137 368313 41 git_buf_putc(&buf, (char)c);
138 368309 42 c = line[++rpos];
139 368309 42 } while (line + rpos < last_quote);
140 -
141 - end_parse:
142 30823 43,44 if (git_buf_oom(&buf))
143 ##### 45 goto end_error;
144 -
145 30823 46,47 if (line[rpos] != '"' || line[rpos + 1] != ']') {
146 3 48 set_parse_error(reader, rpos, "unexpected text after closing quotes");
147 3 49 git_buf_dispose(&buf);
148 3 50 return -1;
149 - }
150 -
151 30820 51 *section_name = git_buf_detach(&buf);
152 30822 52 return (int)(&line[rpos + 2] - line_start); /* rpos is at the closing quote */
153 -
154 - end_error:
155 4 54 git_buf_dispose(&buf);
156 -
157 4 55 return -1;
158 - }
159 -
160 - 2 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files)static int parse_section_header(git_config_parser *reader, char **section_out)
161 - {
162 - char *name, *name_end;
163 - int name_length, c, pos;
164 - int result;
165 - char *line;
166 - size_t line_len;
167 -
168 - 2 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) git_parse_advance_ws(&reader->ctx);
169 - 3 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) line = git__strndup(reader->ctx.line, reader->ctx.line_len);
170 - 4 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) if (line == NULL)
171 - 5 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) return -1;
172 -
173 - /* find the end of the variable's name */
174 - 6 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) name_end = strrchr(line, ']');
175 - 6 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) if (name_end == NULL) {
176 - 7 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) git__free(line);
177 - 8 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) set_parse_error(reader, 0, "missing ']' in section header");
178 - 9 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) return -1;
179 - }
180 -
181 - 10-16 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) GIT_ERROR_CHECK_ALLOC_ADD(&line_len, (size_t)(name_end - line), 1);
182 - 17 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) name = git__malloc(line_len);
183 - 18,19 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) GIT_ERROR_CHECK_ALLOC(name);
184 -
185 - 20 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) name_length = 0;
186 - 20 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) pos = 0;
187 -
188 - /* Make sure we were given a section header */
189 - 20 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) c = line[pos++];
190 - 20,21 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) assert(c == '[');
191 -
192 - 22 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) c = line[pos++];
193 -
194 - do {
195 - 23,24 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) if (git__isspace(c)){
196 - 25 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) name[name_length] = '\0';
197 - 25 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) result = parse_subsection_header(reader, line, pos, name, section_out);
198 - 26 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) git__free(line);
199 - 27 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) git__free(name);
200 - 28 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) return result;
201 - }
202 -
203 - 29-31 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) if (!config_keychar(c) && c != '.') {
204 - 32 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) set_parse_error(reader, pos, "unexpected character in header");
205 - 39 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) goto fail_parse;
206 - }
207 -
208 - 33 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) name[name_length++] = (char)git__tolower(c);
209 -
210 - 33 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) } while ((c = line[pos++]) != ']');
211 -
212 - 34 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) if (line[pos - 1] != ']') {
213 - 35 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) set_parse_error(reader, pos, "unexpected end of file");
214 - 36 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) goto fail_parse;
215 - }
216 -
217 - 37 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) git__free(line);
218 -
219 - 38 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) name[name_length] = 0;
220 - 38 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) *section_out = name;
221 -
222 - 38 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) return pos;
223 -
224 - fail_parse:
225 - 40 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) git__free(line);
226 - 41 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) git__free(name);
227 - 42 suppressed: function cannot be solved parse_section_header (automatic due to inconsistent arc counts in .gcda files) return -1;
228 - }
229 -
230 22971 2 static int skip_bom(git_parse_ctx *parser)
231 - {
232 22971 2 git_buf buf = GIT_BUF_INIT_CONST(parser->content, parser->content_len);
233 - git_bom_t bom;
234 22971 2 int bom_offset = git_buf_text_detect_bom(&bom, &buf);
235 -
236 22972 3 if (bom == GIT_BOM_UTF8)
237 5 4 git_parse_advance_chars(parser, bom_offset);
238 -
239 - /* TODO: reference implementation is pretty stupid with BoM */
240 -
241 22972 5 return 0;
242 - }
243 -
244 - /*
245 - (* basic types *)
246 - digit = "0".."9"
247 - integer = digit { digit }
248 - alphabet = "a".."z" + "A" .. "Z"
249 -
250 - section_char = alphabet | "." | "-"
251 - extension_char = (* any character except newline *)
252 - any_char = (* any character *)
253 - variable_char = "alphabet" | "-"
254 -
255 -
256 - (* actual grammar *)
257 - config = { section }
258 -
259 - section = header { definition }
260 -
261 - header = "[" section [subsection | subsection_ext] "]"
262 -
263 - subsection = "." section
264 - subsection_ext = "\"" extension "\""
265 -
266 - section = section_char { section_char }
267 - extension = extension_char { extension_char }
268 -
269 - definition = variable_name ["=" variable_value] "\n"
270 -
271 - variable_name = variable_char { variable_char }
272 - variable_value = string | boolean | integer
273 -
274 - string = quoted_string | plain_string
275 - quoted_string = "\"" plain_string "\""
276 - plain_string = { any_char }
277 -
278 - boolean = boolean_true | boolean_false
279 - boolean_true = "yes" | "1" | "true" | "on"
280 - boolean_false = "no" | "0" | "false" | "off"
281 - */
282 -
283 - /* '\"' -> '"' etc */
284 - 2 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files)static int unescape_line(
285 - char **out, bool *is_multi, const char *ptr, int quote_count)
286 - {
287 - char *str, *fixed, *esc;
288 - 2 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) size_t ptr_len = strlen(ptr), alloc_len;
289 -
290 - 2 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) *is_multi = false;
291 -
292 - 2-6 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) if (GIT_ADD_SIZET_OVERFLOW(&alloc_len, ptr_len, 1) ||
293 - 5 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) (str = git__malloc(alloc_len)) == NULL) {
294 - 7 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) return -1;
295 - }
296 -
297 - 8 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) fixed = str;
298 -
299 - 8,21 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) while (*ptr != '\0') {
300 - 9 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) if (*ptr == '"') {
301 - 10 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) quote_count++;
302 - 11 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) } else if (*ptr != '\\') {
303 - 12 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) *fixed++ = *ptr;
304 - } else {
305 - /* backslash, check the next char */
306 - 13 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) ptr++;
307 - /* if we're at the end, it's a multiline, so keep the backslash */
308 - 13 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) if (*ptr == '\0') {
309 - 14 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) *is_multi = true;
310 - 14 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) goto done;
311 - }
312 - 15 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) if ((esc = strchr(git_config_escapes, *ptr)) != NULL) {
313 - 16 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) *fixed++ = git_config_escaped[esc - git_config_escapes];
314 - } else {
315 - 17 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) git__free(str);
316 - 18 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) git_error_set(GIT_ERROR_CONFIG, "invalid escape at %s", ptr);
317 - 19 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) return -1;
318 - }
319 - }
320 - 20 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) ptr++;
321 - }
322 -
323 - done:
324 - 22 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) *fixed = '\0';
325 - 22 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) *out = str;
326 -
327 - 22 suppressed: function cannot be solved unescape_line (automatic due to inconsistent arc counts in .gcda files) return 0;
328 - }
329 -
330 7 2 static int parse_multiline_variable(git_config_parser *reader, git_buf *value, int in_quotes)
331 - {
332 - int quote_count;
333 7 2 bool multiline = true;
334 -
335 16 2,25 while (multiline) {
336 11 3 char *line = NULL, *proc_line = NULL;
337 - int error;
338 -
339 - /* Check that the next line exists */
340 11 3 git_parse_advance_line(&reader->ctx);
341 11 4 line = git__strndup(reader->ctx.line, reader->ctx.line_len);
342 11 5,6,24 GIT_ERROR_CHECK_ALLOC(line);
343 -
344 - /*
345 - * We've reached the end of the file, there is no continuation.
346 - * (this is not an error).
347 - */
348 11 7 if (line[0] == '\0') {
349 2 8 error = 0;
350 2 8 goto out;
351 - }
352 -
353 - /* If it was just a comment, pretend it didn't exist */
354 9 9 quote_count = strip_comments(line, !!in_quotes);
355 9 10 if (line[0] == '\0')
356 ##### 11 goto next;
357 -
358 9 12,13 if ((error = unescape_line(&proc_line, &multiline,
359 - line, in_quotes)) < 0)
360 ##### 14 goto out;
361 -
362 - /* Add this line to the multiline var */
363 9 15,16 if ((error = git_buf_puts(value, proc_line)) < 0)
364 ##### 17 goto out;
365 -
366 - next:
367 9 18 git__free(line);
368 9 19 git__free(proc_line);
369 9 20 in_quotes = quote_count;
370 9 20 continue;
371 -
372 - out:
373 2 21 git__free(line);
374 2 22 git__free(proc_line);
375 2 23 return error;
376 - }
377 -
378 5 26 return 0;
379 - }
380 -
381 1175066 2 GIT_INLINE(bool) is_namechar(char c)
382 - {
383 1175066 2 return isalnum(c) || c == '-';
384 - }
385 -
386 124826 2 static int parse_name(
387 - char **name, const char **value, git_config_parser *reader, const char *line)
388 - {
389 124826 2 const char *name_end = line, *value_start;
390 -
391 124826 2 *name = NULL;
392 124826 2 *value = NULL;
393 -
394 1175153 2,4-6 while (*name_end && is_namechar(*name_end))
395 1050327 3 name_end++;
396 -
397 124789 7 if (line == name_end) {
398 ##### 8 set_parse_error(reader, 0, "invalid configuration key");
399 ##### 9 return -1;
400 - }
401 -
402 124789 10 value_start = name_end;
403 -
404 249414 10,12-14 while (*value_start && git__isspace(*value_start))
405 124625 11 value_start++;
406 -
407 124787 15 if (*value_start == '=') {
408 124776 16 *value = value_start + 1;
409 11 17 } else if (*value_start) {
410 3 18 set_parse_error(reader, 0, "invalid configuration key");
411 3 19 return -1;
412 - }
413 -
414 124792 20,21 if ((*name = git__strndup(line, name_end - line)) == NULL)
415 ##### 22 return -1;
416 -
417 124792 23 return 0;
418 - }
419 -
420 124790 2 static int parse_variable(git_config_parser *reader, char **var_name, char **var_value)
421 - {
422 124790 2 const char *value_start = NULL;
423 124790 2 char *line = NULL, *name = NULL, *value = NULL;
424 - int quote_count, error;
425 - bool multiline;
426 -
427 124790 2 *var_name = NULL;
428 124790 2 *var_value = NULL;
429 -
430 124790 2 git_parse_advance_ws(&reader->ctx);
431 124787 3 line = git__strndup(reader->ctx.line, reader->ctx.line_len);
432 124787 4,5 GIT_ERROR_CHECK_ALLOC(line);
433 -
434 124787 6 quote_count = strip_comments(line, 0);
435 -
436 124792 7,8 if ((error = parse_name(&name, &value_start, reader, line)) < 0)
437 3 9 goto out;
438 -
439 - /*
440 - * Now, let's try to parse the value
441 - */
442 124789 10 if (value_start != NULL) {
443 239683 11,13,14 while (git__isspace(value_start[0]))
444 114900 12 value_start++;
445 -
446 124783 15,16 if ((error = unescape_line(&value, &multiline, value_start, 0)) < 0)
447 1 17 goto out;
448 -
449 124782 18 if (multiline) {
450 7 19 git_buf multi_value = GIT_BUF_INIT;
451 7 19 git_buf_attach(&multi_value, value, 0);
452 7 20 value = NULL;
453 -
454 7 20,21,23 if (parse_multiline_variable(reader, &multi_value, quote_count) < 0 ||
455 7 22 git_buf_oom(&multi_value)) {
456 ##### 24 error = -1;
457 ##### 24 git_buf_dispose(&multi_value);
458 ##### 25 goto out;
459 - }
460 -
461 7 26,27 value = git_buf_detach(&multi_value);
462 - }
463 - }
464 -
465 124788 28 *var_name = name;
466 124788 28 *var_value = value;
467 124788 28 name = NULL;
468 124788 28 value = NULL;
469 -
470 - out:
471 124792 29 git__free(name);
472 124788 30 git__free(value);
473 124773 31 git__free(line);
474 124793 32 return error;
475 - }
476 -
477 4672 2 int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen)
478 - {
479 4672 2 out->path = path;
480 4672 2 return git_parse_ctx_init(&out->ctx, data, datalen);
481 - }
482 -
483 4673 2 void git_config_parser_dispose(git_config_parser *parser)
484 - {
485 4673 2 git_parse_ctx_clear(&parser->ctx);
486 4673 3 }
487 -
488 22975 2 int git_config_parse(
489 - git_config_parser *parser,
490 - git_config_parser_section_cb on_section,
491 - git_config_parser_variable_cb on_variable,
492 - git_config_parser_comment_cb on_comment,
493 - git_config_parser_eof_cb on_eof,
494 - void *payload)
495 - {
496 - git_parse_ctx *ctx;
497 22975 2 char *current_section = NULL, *var_name = NULL, *var_value = NULL;
498 22975 2 int result = 0;
499 -
500 22975 2 ctx = &parser->ctx;
501 -
502 22975 2 skip_bom(ctx);
503 -
504 201844 3,34,35 for (; ctx->remain_len > 0; git_parse_advance_line(ctx)) {
505 - const char *line_start;
506 - size_t line_len;
507 - char c;
508 -
509 - restart:
510 178904 4 line_start = ctx->line;
511 178904 4 line_len = ctx->line_len;
512 -
513 - /*
514 - * Get either first non-whitespace character or, if that does
515 - * not exist, the first whitespace character. This is required
516 - * to preserve whitespaces when writing back the file.
517 - */
518 178917 4,5,7 if (git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE) < 0 &&
519 1397 6 git_parse_peek(&c, ctx, 0) < 0)
520 ##### 8 continue;
521 -
522 178917 9 switch (c) {
523 - case '[': /* section header, new section begins */
524 52700 10 git__free(current_section);
525 52702 11 current_section = NULL;
526 -
527 52702 11 result = parse_section_header(parser, &current_section);
528 52695 12 if (result < 0)
529 3 13 break;
530 -
531 52692 14 git_parse_advance_chars(ctx, result);
532 -
533 52693 15 if (on_section)
534 9423 16 result = on_section(parser, current_section, line_start, line_len, payload);
535 - /*
536 - * After we've parsed the section header we may not be
537 - * done with the line. If there's still data in there,
538 - * run the next loop with the rest of the current line
539 - * instead of moving forward.
540 - */
541 -
542 52693 17,18 if (!git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE))
543 8 19 goto restart;
544 -
545 52683 20 break;
546 -
547 - case '\n': /* comment or whitespace-only */
548 - case '\r':
549 - case ' ':
550 - case '\t':
551 - case ';':
552 - case '#':
553 1426 21 if (on_comment) {
554 218 22 result = on_comment(parser, line_start, line_len, payload);
555 - }
556 1426 23 break;
557 -
558 - default: /* assume variable declaration */
559 124792 24-26 if ((result = parse_variable(parser, &var_name, &var_value)) == 0 && on_variable) {
560 124788 27 result = on_variable(parser, current_section, var_name, var_value, line_start, line_len, payload);
561 124787 28 git__free(var_name);
562 124799 29 git__free(var_value);
563 - }
564 -
565 124800 30 break;
566 - }
567 -
568 178912 31 if (result < 0)
569 178885 32,33 goto out;
570 - }
571 -
572 22948 36 if (on_eof)
573 4664 37 result = on_eof(parser, current_section, payload);
574 -
575 - out:
576 22975 38 git__free(current_section);
577 22977 39 return result;
578 - }