author | Lars Hjemli <hjemli@gmail.com> | 2006-12-11 15:11:40 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2006-12-11 15:11:40 (UTC) |
commit | 51ada4fda2b47710351e6e4da8a95807d6d9f729 (patch) (unidiff) | |
tree | 48b543fd16b666db7024038506ffc4eadb0ca966 | |
parent | 318d106300102c19d114a4ea89265b0a4060d9cb (diff) | |
download | cgit-51ada4fda2b47710351e6e4da8a95807d6d9f729.zip cgit-51ada4fda2b47710351e6e4da8a95807d6d9f729.tar.gz cgit-51ada4fda2b47710351e6e4da8a95807d6d9f729.tar.bz2 |
Rename config.c to parsing.c + move cgit_parse_query from cgit.c to parsing.c
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | cgit.c | 26 | ||||
-rw-r--r-- | cgit.h | 1 | ||||
-rw-r--r-- | parsing.c (renamed from config.c) | 25 |
4 files changed, 29 insertions, 28 deletions
@@ -1,26 +1,27 @@ | |||
1 | CGIT_VERSION = 0.1-pre | 1 | CGIT_VERSION = 0.1-pre |
2 | 2 | ||
3 | INSTALL_BIN = /var/www/htdocs/cgit.cgi | 3 | INSTALL_BIN = /var/www/htdocs/cgit.cgi |
4 | INSTALL_CSS = /var/www/htdocs/cgit.css | 4 | INSTALL_CSS = /var/www/htdocs/cgit.css |
5 | CACHE_ROOT = /var/cache/cgit | 5 | CACHE_ROOT = /var/cache/cgit |
6 | 6 | ||
7 | EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto | 7 | EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto |
8 | OBJECTS = config.o html.o cache.o | 8 | OBJECTS = parsing.o html.o cache.o |
9 | 9 | ||
10 | CFLAGS += -Wall | 10 | CFLAGS += -Wall |
11 | 11 | ||
12 | all: cgit | 12 | all: cgit |
13 | 13 | ||
14 | install: all | 14 | install: all |
15 | install cgit $(INSTALL_BIN) | 15 | install cgit $(INSTALL_BIN) |
16 | install cgit.css $(INSTALL_CSS) | 16 | install cgit.css $(INSTALL_CSS) |
17 | rm -rf $(CACHE_ROOT)/* | 17 | rm -rf $(CACHE_ROOT)/* |
18 | 18 | ||
19 | cgit: cgit.c cgit.h git.h $(OBJECTS) | 19 | cgit: cgit.c cgit.h git.h $(OBJECTS) |
20 | $(CC) $(CFLAGS) -DCGIT_VERSION='"$(CGIT_VERSION)"' cgit.c -o cgit $(OBJECTS) $(EXTLIBS) | 20 | $(CC) $(CFLAGS) -DCGIT_VERSION='"$(CGIT_VERSION)"' cgit.c -o cgit \ |
21 | $(OBJECTS) $(EXTLIBS) | ||
21 | 22 | ||
22 | $(OBJECTS): cgit.h git.h | 23 | $(OBJECTS): cgit.h git.h |
23 | 24 | ||
24 | .PHONY: clean | 25 | .PHONY: clean |
25 | clean: | 26 | clean: |
26 | rm -f cgit *.o | 27 | rm -f cgit *.o |
@@ -1,273 +1,247 @@ | |||
1 | /* cgit.c: cgi for the git scm | 1 | /* cgit.c: cgi for the git scm |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | 10 | ||
11 | const char cgit_version[] = CGIT_VERSION; | 11 | const char cgit_version[] = CGIT_VERSION; |
12 | 12 | ||
13 | const char cgit_doctype[] = | 13 | const char cgit_doctype[] = |
14 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | 14 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" |
15 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | 15 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
16 | 16 | ||
17 | const char cgit_error[] = | 17 | const char cgit_error[] = |
18 | "<div class='error'>%s</div>"; | 18 | "<div class='error'>%s</div>"; |
19 | 19 | ||
20 | const char cgit_lib_error[] = | 20 | const char cgit_lib_error[] = |
21 | "<div class='error'>%s: %s</div>"; | 21 | "<div class='error'>%s: %s</div>"; |
22 | 22 | ||
23 | int htmlfd = 0; | 23 | int htmlfd = 0; |
24 | 24 | ||
25 | char *cgit_root = "/usr/src/git"; | 25 | char *cgit_root = "/usr/src/git"; |
26 | char *cgit_root_title = "Git repository browser"; | 26 | char *cgit_root_title = "Git repository browser"; |
27 | char *cgit_css = "/cgit.css"; | 27 | char *cgit_css = "/cgit.css"; |
28 | char *cgit_logo = "/git-logo.png"; | 28 | char *cgit_logo = "/git-logo.png"; |
29 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; | 29 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; |
30 | char *cgit_virtual_root = NULL; | 30 | char *cgit_virtual_root = NULL; |
31 | 31 | ||
32 | char *cgit_cache_root = "/var/cache/cgit"; | 32 | char *cgit_cache_root = "/var/cache/cgit"; |
33 | 33 | ||
34 | int cgit_max_lock_attempts = 5; | 34 | int cgit_max_lock_attempts = 5; |
35 | int cgit_cache_root_ttl = 5; | 35 | int cgit_cache_root_ttl = 5; |
36 | int cgit_cache_repo_ttl = 5; | 36 | int cgit_cache_repo_ttl = 5; |
37 | int cgit_cache_dynamic_ttl = 5; | 37 | int cgit_cache_dynamic_ttl = 5; |
38 | int cgit_cache_static_ttl = -1; | 38 | int cgit_cache_static_ttl = -1; |
39 | int cgit_cache_max_create_time = 5; | 39 | int cgit_cache_max_create_time = 5; |
40 | 40 | ||
41 | char *cgit_repo_name = NULL; | 41 | char *cgit_repo_name = NULL; |
42 | char *cgit_repo_desc = NULL; | 42 | char *cgit_repo_desc = NULL; |
43 | char *cgit_repo_owner = NULL; | 43 | char *cgit_repo_owner = NULL; |
44 | 44 | ||
45 | int cgit_query_has_symref = 0; | 45 | int cgit_query_has_symref = 0; |
46 | int cgit_query_has_sha1 = 0; | 46 | int cgit_query_has_sha1 = 0; |
47 | 47 | ||
48 | char *cgit_querystring = NULL; | 48 | char *cgit_querystring = NULL; |
49 | char *cgit_query_repo = NULL; | 49 | char *cgit_query_repo = NULL; |
50 | char *cgit_query_page = NULL; | 50 | char *cgit_query_page = NULL; |
51 | char *cgit_query_head = NULL; | 51 | char *cgit_query_head = NULL; |
52 | char *cgit_query_sha1 = NULL; | 52 | char *cgit_query_sha1 = NULL; |
53 | 53 | ||
54 | struct cacheitem cacheitem; | 54 | struct cacheitem cacheitem; |
55 | 55 | ||
56 | int cgit_parse_query(char *txt, configfn fn) | ||
57 | { | ||
58 | char *t, *value = NULL, c; | ||
59 | |||
60 | if (!txt) | ||
61 | return 0; | ||
62 | |||
63 | t = txt = xstrdup(txt); | ||
64 | |||
65 | while((c=*t) != '\0') { | ||
66 | if (c=='=') { | ||
67 | *t = '\0'; | ||
68 | value = t+1; | ||
69 | } else if (c=='&') { | ||
70 | *t = '\0'; | ||
71 | (*fn)(txt, value); | ||
72 | txt = t+1; | ||
73 | value = NULL; | ||
74 | } | ||
75 | t++; | ||
76 | } | ||
77 | if (t!=txt) | ||
78 | (*fn)(txt, value); | ||
79 | return 0; | ||
80 | } | ||
81 | |||
82 | void cgit_global_config_cb(const char *name, const char *value) | 56 | void cgit_global_config_cb(const char *name, const char *value) |
83 | { | 57 | { |
84 | if (!strcmp(name, "root")) | 58 | if (!strcmp(name, "root")) |
85 | cgit_root = xstrdup(value); | 59 | cgit_root = xstrdup(value); |
86 | else if (!strcmp(name, "root-title")) | 60 | else if (!strcmp(name, "root-title")) |
87 | cgit_root_title = xstrdup(value); | 61 | cgit_root_title = xstrdup(value); |
88 | else if (!strcmp(name, "css")) | 62 | else if (!strcmp(name, "css")) |
89 | cgit_css = xstrdup(value); | 63 | cgit_css = xstrdup(value); |
90 | else if (!strcmp(name, "logo")) | 64 | else if (!strcmp(name, "logo")) |
91 | cgit_logo = xstrdup(value); | 65 | cgit_logo = xstrdup(value); |
92 | else if (!strcmp(name, "logo-link")) | 66 | else if (!strcmp(name, "logo-link")) |
93 | cgit_logo_link = xstrdup(value); | 67 | cgit_logo_link = xstrdup(value); |
94 | else if (!strcmp(name, "virtual-root")) | 68 | else if (!strcmp(name, "virtual-root")) |
95 | cgit_virtual_root = xstrdup(value); | 69 | cgit_virtual_root = xstrdup(value); |
96 | } | 70 | } |
97 | 71 | ||
98 | void cgit_repo_config_cb(const char *name, const char *value) | 72 | void cgit_repo_config_cb(const char *name, const char *value) |
99 | { | 73 | { |
100 | if (!strcmp(name, "name")) | 74 | if (!strcmp(name, "name")) |
101 | cgit_repo_name = xstrdup(value); | 75 | cgit_repo_name = xstrdup(value); |
102 | else if (!strcmp(name, "desc")) | 76 | else if (!strcmp(name, "desc")) |
103 | cgit_repo_desc = xstrdup(value); | 77 | cgit_repo_desc = xstrdup(value); |
104 | else if (!strcmp(name, "owner")) | 78 | else if (!strcmp(name, "owner")) |
105 | cgit_repo_owner = xstrdup(value); | 79 | cgit_repo_owner = xstrdup(value); |
106 | } | 80 | } |
107 | 81 | ||
108 | void cgit_querystring_cb(const char *name, const char *value) | 82 | void cgit_querystring_cb(const char *name, const char *value) |
109 | { | 83 | { |
110 | if (!strcmp(name,"r")) | 84 | if (!strcmp(name,"r")) |
111 | cgit_query_repo = xstrdup(value); | 85 | cgit_query_repo = xstrdup(value); |
112 | else if (!strcmp(name, "p")) | 86 | else if (!strcmp(name, "p")) |
113 | cgit_query_page = xstrdup(value); | 87 | cgit_query_page = xstrdup(value); |
114 | else if (!strcmp(name, "h")) { | 88 | else if (!strcmp(name, "h")) { |
115 | cgit_query_head = xstrdup(value); | 89 | cgit_query_head = xstrdup(value); |
116 | cgit_query_has_symref = 1; | 90 | cgit_query_has_symref = 1; |
117 | } else if (!strcmp(name, "id")) { | 91 | } else if (!strcmp(name, "id")) { |
118 | cgit_query_sha1 = xstrdup(value); | 92 | cgit_query_sha1 = xstrdup(value); |
119 | cgit_query_has_sha1 = 1; | 93 | cgit_query_has_sha1 = 1; |
120 | } | 94 | } |
121 | } | 95 | } |
122 | 96 | ||
123 | char *cgit_repourl(const char *reponame) | 97 | char *cgit_repourl(const char *reponame) |
124 | { | 98 | { |
125 | if (cgit_virtual_root) { | 99 | if (cgit_virtual_root) { |
126 | return fmt("%s/%s/", cgit_virtual_root, reponame); | 100 | return fmt("%s/%s/", cgit_virtual_root, reponame); |
127 | } else { | 101 | } else { |
128 | return fmt("?r=%s", reponame); | 102 | return fmt("?r=%s", reponame); |
129 | } | 103 | } |
130 | } | 104 | } |
131 | 105 | ||
132 | char *cgit_pageurl(const char *reponame, const char *pagename, | 106 | char *cgit_pageurl(const char *reponame, const char *pagename, |
133 | const char *query) | 107 | const char *query) |
134 | { | 108 | { |
135 | if (cgit_virtual_root) { | 109 | if (cgit_virtual_root) { |
136 | return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, | 110 | return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, |
137 | pagename, query); | 111 | pagename, query); |
138 | } else { | 112 | } else { |
139 | return fmt("?r=%s&p=%s&%s", reponame, pagename, query); | 113 | return fmt("?r=%s&p=%s&%s", reponame, pagename, query); |
140 | } | 114 | } |
141 | } | 115 | } |
142 | 116 | ||
143 | static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1, | 117 | static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1, |
144 | int flags, void *cb_data) | 118 | int flags, void *cb_data) |
145 | { | 119 | { |
146 | struct commit *commit; | 120 | struct commit *commit; |
147 | char buf[256], *url; | 121 | char buf[256], *url; |
148 | 122 | ||
149 | commit = lookup_commit(sha1); | 123 | commit = lookup_commit(sha1); |
150 | if (commit && !parse_commit(commit)){ | 124 | if (commit && !parse_commit(commit)){ |
151 | html("<tr><td>"); | 125 | html("<tr><td>"); |
152 | url = cgit_pageurl(cgit_query_repo, "log", | 126 | url = cgit_pageurl(cgit_query_repo, "log", |
153 | fmt("h=%s", refname)); | 127 | fmt("h=%s", refname)); |
154 | html_link_open(url, NULL, NULL); | 128 | html_link_open(url, NULL, NULL); |
155 | strncpy(buf, refname, sizeof(buf)); | 129 | strncpy(buf, refname, sizeof(buf)); |
156 | html_txt(buf); | 130 | html_txt(buf); |
157 | html_link_close(); | 131 | html_link_close(); |
158 | html("</td><td>"); | 132 | html("</td><td>"); |
159 | pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf, | 133 | pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf, |
160 | sizeof(buf), 0, NULL, NULL, 0); | 134 | sizeof(buf), 0, NULL, NULL, 0); |
161 | html_txt(buf); | 135 | html_txt(buf); |
162 | html("</td></tr>\n"); | 136 | html("</td></tr>\n"); |
163 | } else { | 137 | } else { |
164 | html("<tr><td>"); | 138 | html("<tr><td>"); |
165 | html_txt(buf); | 139 | html_txt(buf); |
166 | html("</td><td>"); | 140 | html("</td><td>"); |
167 | htmlf("*** bad ref %s", sha1_to_hex(sha1)); | 141 | htmlf("*** bad ref %s", sha1_to_hex(sha1)); |
168 | html("</td></tr>\n"); | 142 | html("</td></tr>\n"); |
169 | } | 143 | } |
170 | return 0; | 144 | return 0; |
171 | } | 145 | } |
172 | 146 | ||
173 | /* Sun, 06 Nov 1994 08:49:37 GMT */ | 147 | /* Sun, 06 Nov 1994 08:49:37 GMT */ |
174 | static char *http_date(time_t t) | 148 | static char *http_date(time_t t) |
175 | { | 149 | { |
176 | static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | 150 | static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
177 | static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | 151 | static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
178 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; | 152 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; |
179 | struct tm *tm = gmtime(&t); | 153 | struct tm *tm = gmtime(&t); |
180 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], | 154 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], |
181 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, | 155 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, |
182 | tm->tm_hour, tm->tm_min, tm->tm_sec); | 156 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
183 | } | 157 | } |
184 | 158 | ||
185 | static int ttl_seconds(int ttl) | 159 | static int ttl_seconds(int ttl) |
186 | { | 160 | { |
187 | if (ttl<0) | 161 | if (ttl<0) |
188 | return 60 * 60 * 24 * 365; | 162 | return 60 * 60 * 24 * 365; |
189 | else | 163 | else |
190 | return ttl * 60; | 164 | return ttl * 60; |
191 | } | 165 | } |
192 | 166 | ||
193 | static void cgit_print_docstart(char *title) | 167 | static void cgit_print_docstart(char *title) |
194 | { | 168 | { |
195 | html("Content-Type: text/html; charset=utf-8\n"); | 169 | html("Content-Type: text/html; charset=utf-8\n"); |
196 | htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime)); | 170 | htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime)); |
197 | htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime + | 171 | htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime + |
198 | ttl_seconds(cacheitem.ttl))); | 172 | ttl_seconds(cacheitem.ttl))); |
199 | html("\n"); | 173 | html("\n"); |
200 | html(cgit_doctype); | 174 | html(cgit_doctype); |
201 | html("<html>\n"); | 175 | html("<html>\n"); |
202 | html("<head>\n"); | 176 | html("<head>\n"); |
203 | html("<title>"); | 177 | html("<title>"); |
204 | html_txt(title); | 178 | html_txt(title); |
205 | html("</title>\n"); | 179 | html("</title>\n"); |
206 | htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version); | 180 | htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version); |
207 | html("<link rel='stylesheet' type='text/css' href='"); | 181 | html("<link rel='stylesheet' type='text/css' href='"); |
208 | html_attr(cgit_css); | 182 | html_attr(cgit_css); |
209 | html("'/>\n"); | 183 | html("'/>\n"); |
210 | html("</head>\n"); | 184 | html("</head>\n"); |
211 | html("<body>\n"); | 185 | html("<body>\n"); |
212 | } | 186 | } |
213 | 187 | ||
214 | static void cgit_print_docend() | 188 | static void cgit_print_docend() |
215 | { | 189 | { |
216 | html("</body>\n</html>\n"); | 190 | html("</body>\n</html>\n"); |
217 | } | 191 | } |
218 | 192 | ||
219 | static void cgit_print_pageheader(char *title) | 193 | static void cgit_print_pageheader(char *title) |
220 | { | 194 | { |
221 | html("<div id='header'>"); | 195 | html("<div id='header'>"); |
222 | htmlf("<a href='%s'>", cgit_logo_link); | 196 | htmlf("<a href='%s'>", cgit_logo_link); |
223 | htmlf("<img id='logo' src='%s'/>\n", cgit_logo); | 197 | htmlf("<img id='logo' src='%s'/>\n", cgit_logo); |
224 | htmlf("</a>"); | 198 | htmlf("</a>"); |
225 | html_txt(title); | 199 | html_txt(title); |
226 | html("</div>"); | 200 | html("</div>"); |
227 | } | 201 | } |
228 | 202 | ||
229 | static void cgit_print_repolist() | 203 | static void cgit_print_repolist() |
230 | { | 204 | { |
231 | DIR *d; | 205 | DIR *d; |
232 | struct dirent *de; | 206 | struct dirent *de; |
233 | struct stat st; | 207 | struct stat st; |
234 | char *name; | 208 | char *name; |
235 | 209 | ||
236 | chdir(cgit_root); | 210 | chdir(cgit_root); |
237 | cgit_print_docstart(cgit_root_title); | 211 | cgit_print_docstart(cgit_root_title); |
238 | cgit_print_pageheader(cgit_root_title); | 212 | cgit_print_pageheader(cgit_root_title); |
239 | 213 | ||
240 | if (!(d = opendir("."))) { | 214 | if (!(d = opendir("."))) { |
241 | htmlf(cgit_lib_error, "Unable to scan repository directory", | 215 | htmlf(cgit_lib_error, "Unable to scan repository directory", |
242 | strerror(errno)); | 216 | strerror(errno)); |
243 | cgit_print_docend(); | 217 | cgit_print_docend(); |
244 | return; | 218 | return; |
245 | } | 219 | } |
246 | 220 | ||
247 | html("<h2>Repositories</h2>\n"); | 221 | html("<h2>Repositories</h2>\n"); |
248 | html("<table class='list'>"); | 222 | html("<table class='list'>"); |
249 | html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n"); | 223 | html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n"); |
250 | while ((de = readdir(d)) != NULL) { | 224 | while ((de = readdir(d)) != NULL) { |
251 | if (de->d_name[0] == '.') | 225 | if (de->d_name[0] == '.') |
252 | continue; | 226 | continue; |
253 | if (stat(de->d_name, &st) < 0) | 227 | if (stat(de->d_name, &st) < 0) |
254 | continue; | 228 | continue; |
255 | if (!S_ISDIR(st.st_mode)) | 229 | if (!S_ISDIR(st.st_mode)) |
256 | continue; | 230 | continue; |
257 | 231 | ||
258 | cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL; | 232 | cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL; |
259 | name = fmt("%s/info/cgit", de->d_name); | 233 | name = fmt("%s/info/cgit", de->d_name); |
260 | if (cgit_read_config(name, cgit_repo_config_cb)) | 234 | if (cgit_read_config(name, cgit_repo_config_cb)) |
261 | continue; | 235 | continue; |
262 | 236 | ||
263 | html("<tr><td>"); | 237 | html("<tr><td>"); |
264 | html_link_open(cgit_repourl(de->d_name), NULL, NULL); | 238 | html_link_open(cgit_repourl(de->d_name), NULL, NULL); |
265 | html_txt(cgit_repo_name); | 239 | html_txt(cgit_repo_name); |
266 | html_link_close(); | 240 | html_link_close(); |
267 | html("</td><td>"); | 241 | html("</td><td>"); |
268 | html_txt(cgit_repo_desc); | 242 | html_txt(cgit_repo_desc); |
269 | html("</td><td>"); | 243 | html("</td><td>"); |
270 | html_txt(cgit_repo_owner); | 244 | html_txt(cgit_repo_owner); |
271 | html("</td></tr>\n"); | 245 | html("</td></tr>\n"); |
272 | } | 246 | } |
273 | closedir(d); | 247 | closedir(d); |
@@ -1,66 +1,67 @@ | |||
1 | #ifndef CGIT_H | 1 | #ifndef CGIT_H |
2 | #define CGIT_H | 2 | #define CGIT_H |
3 | 3 | ||
4 | #include "git.h" | 4 | #include "git.h" |
5 | #include <openssl/sha.h> | 5 | #include <openssl/sha.h> |
6 | #include <ctype.h> | 6 | #include <ctype.h> |
7 | #include <sched.h> | 7 | #include <sched.h> |
8 | 8 | ||
9 | typedef void (*configfn)(const char *name, const char *value); | 9 | typedef void (*configfn)(const char *name, const char *value); |
10 | 10 | ||
11 | struct cacheitem { | 11 | struct cacheitem { |
12 | char *name; | 12 | char *name; |
13 | struct stat st; | 13 | struct stat st; |
14 | int ttl; | 14 | int ttl; |
15 | int fd; | 15 | int fd; |
16 | }; | 16 | }; |
17 | 17 | ||
18 | extern char *cgit_root; | 18 | extern char *cgit_root; |
19 | extern char *cgit_root_title; | 19 | extern char *cgit_root_title; |
20 | extern char *cgit_css; | 20 | extern char *cgit_css; |
21 | extern char *cgit_logo; | 21 | extern char *cgit_logo; |
22 | extern char *cgit_logo_link; | 22 | extern char *cgit_logo_link; |
23 | extern char *cgit_virtual_root; | 23 | extern char *cgit_virtual_root; |
24 | extern char *cgit_cache_root; | 24 | extern char *cgit_cache_root; |
25 | 25 | ||
26 | extern int cgit_max_lock_attempts; | 26 | extern int cgit_max_lock_attempts; |
27 | extern int cgit_cache_root_ttl; | 27 | extern int cgit_cache_root_ttl; |
28 | extern int cgit_cache_repo_ttl; | 28 | extern int cgit_cache_repo_ttl; |
29 | extern int cgit_cache_dynamic_ttl; | 29 | extern int cgit_cache_dynamic_ttl; |
30 | extern int cgit_cache_static_ttl; | 30 | extern int cgit_cache_static_ttl; |
31 | extern int cgit_cache_max_create_time; | 31 | extern int cgit_cache_max_create_time; |
32 | 32 | ||
33 | extern char *cgit_repo_name; | 33 | extern char *cgit_repo_name; |
34 | extern char *cgit_repo_desc; | 34 | extern char *cgit_repo_desc; |
35 | extern char *cgit_repo_owner; | 35 | extern char *cgit_repo_owner; |
36 | 36 | ||
37 | extern int cgit_query_has_symref; | 37 | extern int cgit_query_has_symref; |
38 | extern int cgit_query_has_sha1; | 38 | extern int cgit_query_has_sha1; |
39 | 39 | ||
40 | extern char *cgit_querystring; | 40 | extern char *cgit_querystring; |
41 | extern char *cgit_query_repo; | 41 | extern char *cgit_query_repo; |
42 | extern char *cgit_query_page; | 42 | extern char *cgit_query_page; |
43 | extern char *cgit_query_head; | 43 | extern char *cgit_query_head; |
44 | extern char *cgit_query_sha1; | 44 | extern char *cgit_query_sha1; |
45 | 45 | ||
46 | extern int htmlfd; | 46 | extern int htmlfd; |
47 | 47 | ||
48 | extern char *fmt(const char *format,...); | 48 | extern char *fmt(const char *format,...); |
49 | 49 | ||
50 | extern void html(const char *txt); | 50 | extern void html(const char *txt); |
51 | extern void htmlf(const char *format,...); | 51 | extern void htmlf(const char *format,...); |
52 | extern void html_txt(char *txt); | 52 | extern void html_txt(char *txt); |
53 | extern void html_attr(char *txt); | 53 | extern void html_attr(char *txt); |
54 | extern void html_link_open(char *url, char *title, char *class); | 54 | extern void html_link_open(char *url, char *title, char *class); |
55 | extern void html_link_close(void); | 55 | extern void html_link_close(void); |
56 | 56 | ||
57 | 57 | ||
58 | extern int cgit_read_config(const char *filename, configfn fn); | 58 | extern int cgit_read_config(const char *filename, configfn fn); |
59 | extern int cgit_parse_query(char *txt, configfn fn); | ||
59 | 60 | ||
60 | extern void cache_prepare(struct cacheitem *item); | 61 | extern void cache_prepare(struct cacheitem *item); |
61 | extern int cache_lock(struct cacheitem *item); | 62 | extern int cache_lock(struct cacheitem *item); |
62 | extern int cache_unlock(struct cacheitem *item); | 63 | extern int cache_unlock(struct cacheitem *item); |
63 | extern int cache_exist(struct cacheitem *item); | 64 | extern int cache_exist(struct cacheitem *item); |
64 | extern int cache_expired(struct cacheitem *item); | 65 | extern int cache_expired(struct cacheitem *item); |
65 | 66 | ||
66 | #endif /* CGIT_H */ | 67 | #endif /* CGIT_H */ |
@@ -1,81 +1,106 @@ | |||
1 | /* config.c: parsing of config files | 1 | /* config.c: parsing of config files |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | 10 | ||
11 | int next_char(FILE *f) | 11 | int next_char(FILE *f) |
12 | { | 12 | { |
13 | int c = fgetc(f); | 13 | int c = fgetc(f); |
14 | if (c=='\r') { | 14 | if (c=='\r') { |
15 | c = fgetc(f); | 15 | c = fgetc(f); |
16 | if (c!='\n') { | 16 | if (c!='\n') { |
17 | ungetc(c, f); | 17 | ungetc(c, f); |
18 | c = '\r'; | 18 | c = '\r'; |
19 | } | 19 | } |
20 | } | 20 | } |
21 | return c; | 21 | return c; |
22 | } | 22 | } |
23 | 23 | ||
24 | void skip_line(FILE *f) | 24 | void skip_line(FILE *f) |
25 | { | 25 | { |
26 | int c; | 26 | int c; |
27 | 27 | ||
28 | while((c=next_char(f)) && c!='\n' && c!=EOF) | 28 | while((c=next_char(f)) && c!='\n' && c!=EOF) |
29 | ; | 29 | ; |
30 | } | 30 | } |
31 | 31 | ||
32 | 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) |
33 | { | 33 | { |
34 | int i = 0, isname = 0; | 34 | int i = 0, isname = 0; |
35 | 35 | ||
36 | *value = NULL; | 36 | *value = NULL; |
37 | while(i<bufsize-1) { | 37 | while(i<bufsize-1) { |
38 | int c = next_char(f); | 38 | int c = next_char(f); |
39 | if (!isname && (c=='#' || c==';')) { | 39 | if (!isname && (c=='#' || c==';')) { |
40 | skip_line(f); | 40 | skip_line(f); |
41 | continue; | 41 | continue; |
42 | } | 42 | } |
43 | if (!isname && isspace(c)) | 43 | if (!isname && isspace(c)) |
44 | continue; | 44 | continue; |
45 | 45 | ||
46 | if (c=='=' && !*value) { | 46 | if (c=='=' && !*value) { |
47 | line[i] = 0; | 47 | line[i] = 0; |
48 | *value = &line[i+1]; | 48 | *value = &line[i+1]; |
49 | } else if (c=='\n' && !isname) { | 49 | } else if (c=='\n' && !isname) { |
50 | i = 0; | 50 | i = 0; |
51 | continue; | 51 | continue; |
52 | } else if (c=='\n' || c==EOF) { | 52 | } else if (c=='\n' || c==EOF) { |
53 | line[i] = 0; | 53 | line[i] = 0; |
54 | break; | 54 | break; |
55 | } else { | 55 | } else { |
56 | line[i]=c; | 56 | line[i]=c; |
57 | } | 57 | } |
58 | isname = 1; | 58 | isname = 1; |
59 | i++; | 59 | i++; |
60 | } | 60 | } |
61 | line[i+1] = 0; | 61 | line[i+1] = 0; |
62 | return i; | 62 | return i; |
63 | } | 63 | } |
64 | 64 | ||
65 | int cgit_read_config(const char *filename, configfn fn) | 65 | int cgit_read_config(const char *filename, configfn fn) |
66 | { | 66 | { |
67 | int ret = 0, len; | 67 | int ret = 0, len; |
68 | char line[256]; | 68 | char line[256]; |
69 | const char *value; | 69 | const char *value; |
70 | FILE *f = fopen(filename, "r"); | 70 | FILE *f = fopen(filename, "r"); |
71 | 71 | ||
72 | if (!f) | 72 | if (!f) |
73 | return -1; | 73 | return -1; |
74 | 74 | ||
75 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) | 75 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) |
76 | (*fn)(line, value); | 76 | (*fn)(line, value); |
77 | 77 | ||
78 | fclose(f); | 78 | fclose(f); |
79 | return ret; | 79 | return ret; |
80 | } | 80 | } |
81 | 81 | ||
82 | int cgit_parse_query(char *txt, configfn fn) | ||
83 | { | ||
84 | char *t, *value = NULL, c; | ||
85 | |||
86 | if (!txt) | ||
87 | return 0; | ||
88 | |||
89 | t = txt = xstrdup(txt); | ||
90 | |||
91 | while((c=*t) != '\0') { | ||
92 | if (c=='=') { | ||
93 | *t = '\0'; | ||
94 | value = t+1; | ||
95 | } else if (c=='&') { | ||
96 | *t = '\0'; | ||
97 | (*fn)(txt, value); | ||
98 | txt = t+1; | ||
99 | value = NULL; | ||
100 | } | ||
101 | t++; | ||
102 | } | ||
103 | if (t!=txt) | ||
104 | (*fn)(txt, value); | ||
105 | return 0; | ||
106 | } | ||