blob: 5b0d880cb7d2d1da755b2f4bc621a6c460ef4df0 [file] [log] [blame]
Lars Hjemli20a33542008-03-28 00:09:11 +01001/* configfile.c: parsing of config files
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli20a33542008-03-28 00:09:11 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
John Keepingf2e8ca82015-08-13 12:14:16 +01009#include <git-compat-util.h>
Lars Hjemli20a33542008-03-28 00:09:11 +010010#include "configfile.h"
11
Lukas Fleischerbafab422013-03-04 08:52:33 +010012static int next_char(FILE *f)
Lars Hjemli20a33542008-03-28 00:09:11 +010013{
14 int c = fgetc(f);
Lukas Fleischer53bc7472013-03-03 16:04:29 +010015 if (c == '\r') {
Lars Hjemli20a33542008-03-28 00:09:11 +010016 c = fgetc(f);
Lukas Fleischer53bc7472013-03-03 16:04:29 +010017 if (c != '\n') {
Lars Hjemli20a33542008-03-28 00:09:11 +010018 ungetc(c, f);
19 c = '\r';
20 }
21 }
22 return c;
23}
24
Lukas Fleischerbafab422013-03-04 08:52:33 +010025static void skip_line(FILE *f)
Lars Hjemli20a33542008-03-28 00:09:11 +010026{
27 int c;
28
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050029 while ((c = next_char(f)) && c != '\n' && c != EOF)
Lars Hjemli20a33542008-03-28 00:09:11 +010030 ;
31}
32
Lukas Fleischer50e70d32013-06-04 14:47:53 +000033static int read_config_line(FILE *f, struct strbuf *name, struct strbuf *value)
Lars Hjemli20a33542008-03-28 00:09:11 +010034{
Lukas Fleischer50e70d32013-06-04 14:47:53 +000035 int c = next_char(f);
Lars Hjemli20a33542008-03-28 00:09:11 +010036
Lukas Fleischer50e70d32013-06-04 14:47:53 +000037 strbuf_reset(name);
38 strbuf_reset(value);
39
40 /* Skip comments and preceding spaces. */
41 for(;;) {
42 if (c == '#' || c == ';')
Lars Hjemli20a33542008-03-28 00:09:11 +010043 skip_line(f);
Lukas Fleischer50e70d32013-06-04 14:47:53 +000044 else if (!isspace(c))
Lars Hjemli20a33542008-03-28 00:09:11 +010045 break;
Lukas Fleischer50e70d32013-06-04 14:47:53 +000046 c = next_char(f);
Lars Hjemli20a33542008-03-28 00:09:11 +010047 }
Lukas Fleischer50e70d32013-06-04 14:47:53 +000048
49 /* Read variable name. */
50 while (c != '=') {
51 if (c == '\n' || c == EOF)
52 return 0;
53 strbuf_addch(name, c);
54 c = next_char(f);
55 }
56
57 /* Read variable value. */
58 c = next_char(f);
59 while (c != '\n' && c != EOF) {
60 strbuf_addch(value, c);
61 c = next_char(f);
62 }
63
64 return 1;
Lars Hjemli20a33542008-03-28 00:09:11 +010065}
66
67int parse_configfile(const char *filename, configfile_value_fn fn)
68{
69 static int nesting;
Lukas Fleischer50e70d32013-06-04 14:47:53 +000070 struct strbuf name = STRBUF_INIT;
71 struct strbuf value = STRBUF_INIT;
Lars Hjemli20a33542008-03-28 00:09:11 +010072 FILE *f;
73
74 /* cancel deeply nested include-commands */
75 if (nesting > 8)
76 return -1;
77 if (!(f = fopen(filename, "r")))
78 return -1;
79 nesting++;
Lukas Fleischer50e70d32013-06-04 14:47:53 +000080 while (read_config_line(f, &name, &value))
81 fn(name.buf, value.buf);
Lars Hjemli20a33542008-03-28 00:09:11 +010082 nesting--;
83 fclose(f);
Lukas Fleischer50e70d32013-06-04 14:47:53 +000084 strbuf_release(&name);
85 strbuf_release(&value);
Lars Hjemli20a33542008-03-28 00:09:11 +010086 return 0;
87}
88