1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
#include "cgit.h"
#include "config.h"
#define MAX_INCLUDE_NESTING 8
struct config_origin config_origin;
static int lines_read;
static int next_char(FILE *f)
{
int c = fgetc(f);
if (c == '\r') {
c = fgetc(f);
if (c != '\n') {
ungetc(c, f);
c = '\r';
}
}
if (c == '\n')
lines_read++;
return c;
}
static void skip_line(FILE *f)
{
int c;
while ((c = next_char(f)) != '\n' && c != EOF)
;
}
static int next_content_char(FILE *f)
{
int c = next_char(f);
for (;;) {
if (c == EOF)
return EOF;
if (c == '#' || c == ';')
skip_line(f);
else if (!isspace(c))
return c;
c = next_char(f);
}
}
static int read_entry(FILE *f, struct strbuf *name, struct strbuf *value, int *line)
{
int c;
strbuf_reset(value);
for (;;) {
c = next_content_char(f);
if (c == EOF)
return 0;
*line = lines_read + 1;
strbuf_reset(name);
while (c != '=' && c != '\n' && c != EOF) {
strbuf_addch(name, c);
c = next_char(f);
}
if (c == EOF)
return 0;
if (c != '=')
continue;
c = next_char(f);
while (c != '\n' && c != EOF) {
strbuf_addch(value, c);
c = next_char(f);
}
return 1;
}
}
int config_file_parse(const char *filename, config_file_value_fn fn)
{
static int nesting;
struct config_origin outer = config_origin;
int outer_lines_read = lines_read;
struct strbuf name = STRBUF_INIT;
struct strbuf value = STRBUF_INIT;
FILE *f;
int err = 0;
if (nesting > MAX_INCLUDE_NESTING)
return 0;
if (!(f = fopen(filename, "r")))
return errno;
config_origin.file = filename;
lines_read = 0;
nesting++;
while (read_entry(f, &name, &value, &config_origin.line))
fn(name.buf, value.buf);
if (ferror(f))
err = errno;
nesting--;
fclose(f);
config_origin = outer;
lines_read = outer_lines_read;
strbuf_release(&name);
strbuf_release(&value);
return err;
}
int config_int(const char *name, const char *value)
{
int number = atoi(value);
char *end;
strtol(value, &end, 10);
while (end != value && isspace(*end))
end++;
if (end != value && !*end)
return number;
if (config_origin.file)
fprintf(stderr, "[cgit] Reading %s=%.*s in %s line %d as %d, since it is not a whole number\n",
name, (int)strcspn(value, "\n"), value, config_origin.file,
config_origin.line, number);
else
fprintf(stderr, "[cgit] Reading %s=%.*s as %d, since it is not a whole number\n",
name, (int)strcspn(value, "\n"), value, number);
return number;
}
|