Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 1 | /* configfile.c: parsing of config files |
| 2 | * |
Lukas Fleischer | f7f26f8 | 2014-01-08 15:10:49 +0100 | [diff] [blame] | 3 | * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
John Keeping | f2e8ca8 | 2015-08-13 12:14:16 +0100 | [diff] [blame] | 9 | #include <git-compat-util.h> |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 10 | #include "configfile.h" |
| 11 | |
Lukas Fleischer | bafab42 | 2013-03-04 08:52:33 +0100 | [diff] [blame] | 12 | static int next_char(FILE *f) |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 13 | { |
| 14 | int c = fgetc(f); |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 15 | if (c == '\r') { |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 16 | c = fgetc(f); |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 17 | if (c != '\n') { |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 18 | ungetc(c, f); |
| 19 | c = '\r'; |
| 20 | } |
| 21 | } |
| 22 | return c; |
| 23 | } |
| 24 | |
Lukas Fleischer | bafab42 | 2013-03-04 08:52:33 +0100 | [diff] [blame] | 25 | static void skip_line(FILE *f) |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 26 | { |
| 27 | int c; |
| 28 | |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 29 | while ((c = next_char(f)) && c != '\n' && c != EOF) |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 30 | ; |
| 31 | } |
| 32 | |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 33 | static int read_config_line(FILE *f, struct strbuf *name, struct strbuf *value) |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 34 | { |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 35 | int c = next_char(f); |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 36 | |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 37 | strbuf_reset(name); |
| 38 | strbuf_reset(value); |
| 39 | |
| 40 | /* Skip comments and preceding spaces. */ |
| 41 | for(;;) { |
John Keeping | 35df710 | 2016-08-07 15:54:14 +0100 | [diff] [blame] | 42 | if (c == EOF) |
| 43 | return 0; |
| 44 | else if (c == '#' || c == ';') |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 45 | skip_line(f); |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 46 | else if (!isspace(c)) |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 47 | break; |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 48 | c = next_char(f); |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 49 | } |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 50 | |
| 51 | /* Read variable name. */ |
| 52 | while (c != '=') { |
| 53 | if (c == '\n' || c == EOF) |
| 54 | return 0; |
| 55 | strbuf_addch(name, c); |
| 56 | c = next_char(f); |
| 57 | } |
| 58 | |
| 59 | /* Read variable value. */ |
| 60 | c = next_char(f); |
| 61 | while (c != '\n' && c != EOF) { |
| 62 | strbuf_addch(value, c); |
| 63 | c = next_char(f); |
| 64 | } |
| 65 | |
| 66 | return 1; |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | int parse_configfile(const char *filename, configfile_value_fn fn) |
| 70 | { |
| 71 | static int nesting; |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 72 | struct strbuf name = STRBUF_INIT; |
| 73 | struct strbuf value = STRBUF_INIT; |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 74 | FILE *f; |
| 75 | |
| 76 | /* cancel deeply nested include-commands */ |
| 77 | if (nesting > 8) |
| 78 | return -1; |
| 79 | if (!(f = fopen(filename, "r"))) |
| 80 | return -1; |
| 81 | nesting++; |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 82 | while (read_config_line(f, &name, &value)) |
| 83 | fn(name.buf, value.buf); |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 84 | nesting--; |
| 85 | fclose(f); |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 86 | strbuf_release(&name); |
| 87 | strbuf_release(&value); |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |