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 | |
| 9 | #include <ctype.h> |
| 10 | #include <stdio.h> |
| 11 | #include "configfile.h" |
| 12 | |
Lukas Fleischer | bafab42 | 2013-03-04 08:52:33 +0100 | [diff] [blame] | 13 | static int next_char(FILE *f) |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 14 | { |
| 15 | int c = fgetc(f); |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 16 | if (c == '\r') { |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 17 | c = fgetc(f); |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 18 | if (c != '\n') { |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 19 | ungetc(c, f); |
| 20 | c = '\r'; |
| 21 | } |
| 22 | } |
| 23 | return c; |
| 24 | } |
| 25 | |
Lukas Fleischer | bafab42 | 2013-03-04 08:52:33 +0100 | [diff] [blame] | 26 | static void skip_line(FILE *f) |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 27 | { |
| 28 | int c; |
| 29 | |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 30 | while ((c = next_char(f)) && c != '\n' && c != EOF) |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 31 | ; |
| 32 | } |
| 33 | |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 34 | 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] | 35 | { |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 36 | int c = next_char(f); |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 37 | |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 38 | strbuf_reset(name); |
| 39 | strbuf_reset(value); |
| 40 | |
| 41 | /* Skip comments and preceding spaces. */ |
| 42 | for(;;) { |
| 43 | if (c == '#' || c == ';') |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 44 | skip_line(f); |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 45 | else if (!isspace(c)) |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 46 | break; |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 47 | c = next_char(f); |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 48 | } |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 49 | |
| 50 | /* Read variable name. */ |
| 51 | while (c != '=') { |
| 52 | if (c == '\n' || c == EOF) |
| 53 | return 0; |
| 54 | strbuf_addch(name, c); |
| 55 | c = next_char(f); |
| 56 | } |
| 57 | |
| 58 | /* Read variable value. */ |
| 59 | c = next_char(f); |
| 60 | while (c != '\n' && c != EOF) { |
| 61 | strbuf_addch(value, c); |
| 62 | c = next_char(f); |
| 63 | } |
| 64 | |
| 65 | return 1; |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int parse_configfile(const char *filename, configfile_value_fn fn) |
| 69 | { |
| 70 | static int nesting; |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 71 | struct strbuf name = STRBUF_INIT; |
| 72 | struct strbuf value = STRBUF_INIT; |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 73 | FILE *f; |
| 74 | |
| 75 | /* cancel deeply nested include-commands */ |
| 76 | if (nesting > 8) |
| 77 | return -1; |
| 78 | if (!(f = fopen(filename, "r"))) |
| 79 | return -1; |
| 80 | nesting++; |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 81 | while (read_config_line(f, &name, &value)) |
| 82 | fn(name.buf, value.buf); |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 83 | nesting--; |
| 84 | fclose(f); |
Lukas Fleischer | 50e70d3 | 2013-06-04 14:47:53 +0000 | [diff] [blame] | 85 | strbuf_release(&name); |
| 86 | strbuf_release(&value); |
Lars Hjemli | 20a3354 | 2008-03-28 00:09:11 +0100 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |