aboutsummaryrefslogtreecommitdiffstats
path: root/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'config.c')
-rw-r--r--config.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/config.c b/config.c
new file mode 100644
index 0000000..858ab69
--- /dev/null
+++ b/config.c
@@ -0,0 +1,73 @@
1#include "cgit.h"
2
3int next_char(FILE *f)
4{
5 int c = fgetc(f);
6 if (c=='\r') {
7 c = fgetc(f);
8 if (c!='\n') {
9 ungetc(c, f);
10 c = '\r';
11 }
12 }
13 return c;
14}
15
16void skip_line(FILE *f)
17{
18 int c;
19
20 while((c=next_char(f)) && c!='\n' && c!=EOF)
21 ;
22}
23
24int read_config_line(FILE *f, char *line, const char **value, int bufsize)
25{
26 int i = 0, isname = 0;
27
28 *value = NULL;
29 while(i<bufsize-1) {
30 int c = next_char(f);
31 if (!isname && (c=='#' || c==';')) {
32 skip_line(f);
33 continue;
34 }
35 if (!isname && isblank(c))
36 continue;
37
38 if (c=='=' && !*value) {
39 line[i] = 0;
40 *value = &line[i+1];
41 } else if (c=='\n' && !isname) {
42 i = 0;
43 continue;
44 } else if (c=='\n' || c==EOF) {
45 line[i] = 0;
46 break;
47 } else {
48 line[i]=c;
49 }
50 isname = 1;
51 i++;
52 }
53 line[i+1] = 0;
54 return i;
55}
56
57int cgit_read_config(const char *filename, configfn fn)
58{
59 int ret = 0, len;
60 char line[256];
61 const char *value;
62 FILE *f = fopen(filename, "r");
63
64 if (!f)
65 return -1;
66
67 while(len = read_config_line(f, line, &value, sizeof(line)))
68 (*fn)(line, value);
69
70 fclose(f);
71 return ret;
72}
73