|
diff --git a/config.c b/config.c index ee49b62..871edf2 100644 --- a/ config.c+++ b/ config.c |
|
@@ -1,73 +1,81 @@ |
| |
1 | /* config.c: parsing of config files |
| |
2 | * |
| |
3 | * Copyright (C) 2006 Lars Hjemli |
| |
4 | * |
| |
5 | * Licensed under GNU General Public License v2 |
| |
6 | * (see COPYING for full license text) |
| |
7 | */ |
| |
8 | |
1 | #include "cgit.h" |
9 | #include "cgit.h" |
2 | |
10 | |
3 | int next_char(FILE *f) |
11 | int next_char(FILE *f) |
4 | { |
12 | { |
5 | int c = fgetc(f); |
13 | int c = fgetc(f); |
6 | if (c=='\r') { |
14 | if (c=='\r') { |
7 | c = fgetc(f); |
15 | c = fgetc(f); |
8 | if (c!='\n') { |
16 | if (c!='\n') { |
9 | ungetc(c, f); |
17 | ungetc(c, f); |
10 | c = '\r'; |
18 | c = '\r'; |
11 | } |
19 | } |
12 | } |
20 | } |
13 | return c; |
21 | return c; |
14 | } |
22 | } |
15 | |
23 | |
16 | void skip_line(FILE *f) |
24 | void skip_line(FILE *f) |
17 | { |
25 | { |
18 | int c; |
26 | int c; |
19 | |
27 | |
20 | while((c=next_char(f)) && c!='\n' && c!=EOF) |
28 | while((c=next_char(f)) && c!='\n' && c!=EOF) |
21 | ; |
29 | ; |
22 | } |
30 | } |
23 | |
31 | |
24 | int read_config_line(FILE *f, char *line, const char **value, int bufsize) |
32 | int read_config_line(FILE *f, char *line, const char **value, int bufsize) |
25 | { |
33 | { |
26 | int i = 0, isname = 0; |
34 | int i = 0, isname = 0; |
27 | |
35 | |
28 | *value = NULL; |
36 | *value = NULL; |
29 | while(i<bufsize-1) { |
37 | while(i<bufsize-1) { |
30 | int c = next_char(f); |
38 | int c = next_char(f); |
31 | if (!isname && (c=='#' || c==';')) { |
39 | if (!isname && (c=='#' || c==';')) { |
32 | skip_line(f); |
40 | skip_line(f); |
33 | continue; |
41 | continue; |
34 | } |
42 | } |
35 | if (!isname && isspace(c)) |
43 | if (!isname && isspace(c)) |
36 | continue; |
44 | continue; |
37 | |
45 | |
38 | if (c=='=' && !*value) { |
46 | if (c=='=' && !*value) { |
39 | line[i] = 0; |
47 | line[i] = 0; |
40 | *value = &line[i+1]; |
48 | *value = &line[i+1]; |
41 | } else if (c=='\n' && !isname) { |
49 | } else if (c=='\n' && !isname) { |
42 | i = 0; |
50 | i = 0; |
43 | continue; |
51 | continue; |
44 | } else if (c=='\n' || c==EOF) { |
52 | } else if (c=='\n' || c==EOF) { |
45 | line[i] = 0; |
53 | line[i] = 0; |
46 | break; |
54 | break; |
47 | } else { |
55 | } else { |
48 | line[i]=c; |
56 | line[i]=c; |
49 | } |
57 | } |
50 | isname = 1; |
58 | isname = 1; |
51 | i++; |
59 | i++; |
52 | } |
60 | } |
53 | line[i+1] = 0; |
61 | line[i+1] = 0; |
54 | return i; |
62 | return i; |
55 | } |
63 | } |
56 | |
64 | |
57 | int cgit_read_config(const char *filename, configfn fn) |
65 | int cgit_read_config(const char *filename, configfn fn) |
58 | { |
66 | { |
59 | int ret = 0, len; |
67 | int ret = 0, len; |
60 | char line[256]; |
68 | char line[256]; |
61 | const char *value; |
69 | const char *value; |
62 | FILE *f = fopen(filename, "r"); |
70 | FILE *f = fopen(filename, "r"); |
63 | |
71 | |
64 | if (!f) |
72 | if (!f) |
65 | return -1; |
73 | return -1; |
66 | |
74 | |
67 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) |
75 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) |
68 | (*fn)(line, value); |
76 | (*fn)(line, value); |
69 | |
77 | |
70 | fclose(f); |
78 | fclose(f); |
71 | return ret; |
79 | return ret; |
72 | } |
80 | } |
73 | |
81 | |
|