diff options
author | Lars Hjemli | 2006-12-11 16:38:30 +0100 |
---|---|---|
committer | Lars Hjemli | 2006-12-11 16:38:30 +0100 |
commit | 5a106eb09b9b5e189b96cc736046a92b054f6c7f (patch) | |
tree | 7172123df870c07deb882a4993fa991b31fc9008 | |
parent | Rename config.c to parsing.c + move cgit_parse_query from cgit.c to parsing.c (diff) | |
download | cgit-5a106eb09b9b5e189b96cc736046a92b054f6c7f.tar.gz cgit-5a106eb09b9b5e189b96cc736046a92b054f6c7f.zip |
Move common output-functions into ui-shared.c
While at it, replace the cgit_[lib_]error constants with a proper function Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | cgit.c | 94 | ||||
-rw-r--r-- | cgit.h | 9 | ||||
-rw-r--r-- | ui-shared.c | 76 |
4 files changed, 99 insertions, 82 deletions
diff --git a/Makefile b/Makefile index eab7926..a4291f7 100644 --- a/Makefile +++ b/Makefile | |||
@@ -5,7 +5,7 @@ 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 = parsing.o html.o cache.o | 8 | OBJECTS = parsing.o html.o cache.o ui-shared.o |
9 | 9 | ||
10 | CFLAGS += -Wall | 10 | CFLAGS += -Wall |
11 | 11 | ||
diff --git a/cgit.c b/cgit.c index 5567859..5438fa1 100644 --- a/cgit.c +++ b/cgit.c | |||
@@ -10,16 +10,6 @@ | |||
10 | 10 | ||
11 | const char cgit_version[] = CGIT_VERSION; | 11 | const char cgit_version[] = CGIT_VERSION; |
12 | 12 | ||
13 | const char cgit_doctype[] = | ||
14 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | ||
15 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | ||
16 | |||
17 | const char cgit_error[] = | ||
18 | "<div class='error'>%s</div>"; | ||
19 | |||
20 | const char cgit_lib_error[] = | ||
21 | "<div class='error'>%s: %s</div>"; | ||
22 | |||
23 | int htmlfd = 0; | 13 | int htmlfd = 0; |
24 | 14 | ||
25 | char *cgit_root = "/usr/src/git"; | 15 | char *cgit_root = "/usr/src/git"; |
@@ -144,63 +134,7 @@ static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1, | |||
144 | return 0; | 134 | return 0; |
145 | } | 135 | } |
146 | 136 | ||
147 | /* Sun, 06 Nov 1994 08:49:37 GMT */ | 137 | static void cgit_print_repolist(struct cacheitem *item) |
148 | static char *http_date(time_t t) | ||
149 | { | ||
150 | static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | ||
151 | static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | ||
152 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; | ||
153 | struct tm *tm = gmtime(&t); | ||
154 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], | ||
155 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, | ||
156 | tm->tm_hour, tm->tm_min, tm->tm_sec); | ||
157 | } | ||
158 | |||
159 | static int ttl_seconds(int ttl) | ||
160 | { | ||
161 | if (ttl<0) | ||
162 | return 60 * 60 * 24 * 365; | ||
163 | else | ||
164 | return ttl * 60; | ||
165 | } | ||
166 | |||
167 | static void cgit_print_docstart(char *title) | ||
168 | { | ||
169 | html("Content-Type: text/html; charset=utf-8\n"); | ||
170 | htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime)); | ||
171 | htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime + | ||
172 | ttl_seconds(cacheitem.ttl))); | ||
173 | html("\n"); | ||
174 | html(cgit_doctype); | ||
175 | html("<html>\n"); | ||
176 | html("<head>\n"); | ||
177 | html("<title>"); | ||
178 | html_txt(title); | ||
179 | html("</title>\n"); | ||
180 | htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version); | ||
181 | html("<link rel='stylesheet' type='text/css' href='"); | ||
182 | html_attr(cgit_css); | ||
183 | html("'/>\n"); | ||
184 | html("</head>\n"); | ||
185 | html("<body>\n"); | ||
186 | } | ||
187 | |||
188 | static void cgit_print_docend() | ||
189 | { | ||
190 | html("</body>\n</html>\n"); | ||
191 | } | ||
192 | |||
193 | static void cgit_print_pageheader(char *title) | ||
194 | { | ||
195 | html("<div id='header'>"); | ||
196 | htmlf("<a href='%s'>", cgit_logo_link); | ||
197 | htmlf("<img id='logo' src='%s'/>\n", cgit_logo); | ||
198 | htmlf("</a>"); | ||
199 | html_txt(title); | ||
200 | html("</div>"); | ||
201 | } | ||
202 | |||
203 | static void cgit_print_repolist() | ||
204 | { | 138 | { |
205 | DIR *d; | 139 | DIR *d; |
206 | struct dirent *de; | 140 | struct dirent *de; |
@@ -208,12 +142,12 @@ static void cgit_print_repolist() | |||
208 | char *name; | 142 | char *name; |
209 | 143 | ||
210 | chdir(cgit_root); | 144 | chdir(cgit_root); |
211 | cgit_print_docstart(cgit_root_title); | 145 | cgit_print_docstart(cgit_root_title, item); |
212 | cgit_print_pageheader(cgit_root_title); | 146 | cgit_print_pageheader(cgit_root_title); |
213 | 147 | ||
214 | if (!(d = opendir("."))) { | 148 | if (!(d = opendir("."))) { |
215 | htmlf(cgit_lib_error, "Unable to scan repository directory", | 149 | cgit_print_error(fmt("Unable to scan repository directory: %s", |
216 | strerror(errno)); | 150 | strerror(errno))); |
217 | cgit_print_docend(); | 151 | cgit_print_docend(); |
218 | return; | 152 | return; |
219 | } | 153 | } |
@@ -379,18 +313,18 @@ static void cgit_print_object(char *hex) | |||
379 | unsigned long size; | 313 | unsigned long size; |
380 | 314 | ||
381 | if (get_sha1_hex(hex, sha1)){ | 315 | if (get_sha1_hex(hex, sha1)){ |
382 | htmlf(cgit_error, "Bad hex value"); | 316 | cgit_print_error(fmt("Bad hex value: %s", hex)); |
383 | return; | 317 | return; |
384 | } | 318 | } |
385 | 319 | ||
386 | if (sha1_object_info(sha1, type, NULL)){ | 320 | if (sha1_object_info(sha1, type, NULL)){ |
387 | htmlf(cgit_error, "Bad object name"); | 321 | cgit_print_error("Bad object name"); |
388 | return; | 322 | return; |
389 | } | 323 | } |
390 | 324 | ||
391 | buf = read_sha1_file(sha1, type, &size); | 325 | buf = read_sha1_file(sha1, type, &size); |
392 | if (!buf) { | 326 | if (!buf) { |
393 | htmlf(cgit_error, "Error reading object"); | 327 | cgit_print_error("Error reading object"); |
394 | return; | 328 | return; |
395 | } | 329 | } |
396 | 330 | ||
@@ -402,21 +336,21 @@ static void cgit_print_object(char *hex) | |||
402 | html("</pre>"); | 336 | html("</pre>"); |
403 | } | 337 | } |
404 | 338 | ||
405 | static void cgit_print_repo_page() | 339 | static void cgit_print_repo_page(struct cacheitem *item) |
406 | { | 340 | { |
407 | if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || | 341 | if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || |
408 | cgit_read_config("info/cgit", cgit_repo_config_cb)) { | 342 | cgit_read_config("info/cgit", cgit_repo_config_cb)) { |
409 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); | 343 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); |
410 | cgit_print_docstart(title); | 344 | cgit_print_docstart(title, item); |
411 | cgit_print_pageheader(title); | 345 | cgit_print_pageheader(title); |
412 | htmlf(cgit_lib_error, "Unable to scan repository", | 346 | cgit_print_error(fmt("Unable to scan repository: %s", |
413 | strerror(errno)); | 347 | strerror(errno))); |
414 | cgit_print_docend(); | 348 | cgit_print_docend(); |
415 | return; | 349 | return; |
416 | } | 350 | } |
417 | setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); | 351 | setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); |
418 | char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); | 352 | char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); |
419 | cgit_print_docstart(title); | 353 | cgit_print_docstart(title, item); |
420 | cgit_print_pageheader(title); | 354 | cgit_print_pageheader(title); |
421 | if (!cgit_query_page) | 355 | if (!cgit_query_page) |
422 | cgit_print_repo_summary(); | 356 | cgit_print_repo_summary(); |
@@ -433,9 +367,9 @@ static void cgit_fill_cache(struct cacheitem *item) | |||
433 | htmlfd = item->fd; | 367 | htmlfd = item->fd; |
434 | item->st.st_mtime = time(NULL); | 368 | item->st.st_mtime = time(NULL); |
435 | if (cgit_query_repo) | 369 | if (cgit_query_repo) |
436 | cgit_print_repo_page(); | 370 | cgit_print_repo_page(item); |
437 | else | 371 | else |
438 | cgit_print_repolist(); | 372 | cgit_print_repolist(item); |
439 | } | 373 | } |
440 | 374 | ||
441 | static void cgit_refresh_cache(struct cacheitem *item) | 375 | static void cgit_refresh_cache(struct cacheitem *item) |
diff --git a/cgit.h b/cgit.h index 6c0aa3b..e64fbd7 100644 --- a/cgit.h +++ b/cgit.h | |||
@@ -15,6 +15,8 @@ struct cacheitem { | |||
15 | int fd; | 15 | int fd; |
16 | }; | 16 | }; |
17 | 17 | ||
18 | extern const char cgit_version[]; | ||
19 | |||
18 | extern char *cgit_root; | 20 | extern char *cgit_root; |
19 | extern char *cgit_root_title; | 21 | extern char *cgit_root_title; |
20 | extern char *cgit_css; | 22 | extern char *cgit_css; |
@@ -54,7 +56,6 @@ extern void html_attr(char *txt); | |||
54 | extern void html_link_open(char *url, char *title, char *class); | 56 | extern void html_link_open(char *url, char *title, char *class); |
55 | extern void html_link_close(void); | 57 | extern void html_link_close(void); |
56 | 58 | ||
57 | |||
58 | extern int cgit_read_config(const char *filename, configfn fn); | 59 | extern int cgit_read_config(const char *filename, configfn fn); |
59 | extern int cgit_parse_query(char *txt, configfn fn); | 60 | extern int cgit_parse_query(char *txt, configfn fn); |
60 | 61 | ||
@@ -64,4 +65,10 @@ extern int cache_unlock(struct cacheitem *item); | |||
64 | extern int cache_exist(struct cacheitem *item); | 65 | extern int cache_exist(struct cacheitem *item); |
65 | extern int cache_expired(struct cacheitem *item); | 66 | extern int cache_expired(struct cacheitem *item); |
66 | 67 | ||
68 | extern void cgit_print_error(char *msg); | ||
69 | extern void cgit_print_docstart(char *title, struct cacheitem *item); | ||
70 | extern void cgit_print_docend(); | ||
71 | extern void cgit_print_pageheader(char *title); | ||
72 | |||
73 | |||
67 | #endif /* CGIT_H */ | 74 | #endif /* CGIT_H */ |
diff --git a/ui-shared.c b/ui-shared.c new file mode 100644 index 0000000..e795043 --- /dev/null +++ b/ui-shared.c | |||
@@ -0,0 +1,76 @@ | |||
1 | /* ui-shared.c: common web output functions | ||
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 | |||
9 | #include "cgit.h" | ||
10 | |||
11 | const char cgit_doctype[] = | ||
12 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | ||
13 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | ||
14 | |||
15 | static char *http_date(time_t t) | ||
16 | { | ||
17 | static char day[][4] = | ||
18 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | ||
19 | static char month[][4] = | ||
20 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | ||
21 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; | ||
22 | struct tm *tm = gmtime(&t); | ||
23 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], | ||
24 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, | ||
25 | tm->tm_hour, tm->tm_min, tm->tm_sec); | ||
26 | } | ||
27 | |||
28 | static int ttl_seconds(int ttl) | ||
29 | { | ||
30 | if (ttl<0) | ||
31 | return 60 * 60 * 24 * 365; | ||
32 | else | ||
33 | return ttl * 60; | ||
34 | } | ||
35 | |||
36 | void cgit_print_error(char *msg) | ||
37 | { | ||
38 | html("<div class='error'>"); | ||
39 | html_txt(msg); | ||
40 | html("</div>\n"); | ||
41 | } | ||
42 | void cgit_print_docstart(char *title, struct cacheitem *item) | ||
43 | { | ||
44 | html("Content-Type: text/html; charset=utf-8\n"); | ||
45 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | ||
46 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | ||
47 | ttl_seconds(item->ttl))); | ||
48 | html("\n"); | ||
49 | html(cgit_doctype); | ||
50 | html("<html>\n"); | ||
51 | html("<head>\n"); | ||
52 | html("<title>"); | ||
53 | html_txt(title); | ||
54 | html("</title>\n"); | ||
55 | htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version); | ||
56 | html("<link rel='stylesheet' type='text/css' href='"); | ||
57 | html_attr(cgit_css); | ||
58 | html("'/>\n"); | ||
59 | html("</head>\n"); | ||
60 | html("<body>\n"); | ||
61 | } | ||
62 | |||
63 | void cgit_print_docend() | ||
64 | { | ||
65 | html("</body>\n</html>\n"); | ||
66 | } | ||
67 | |||
68 | void cgit_print_pageheader(char *title) | ||
69 | { | ||
70 | html("<div id='header'>"); | ||
71 | htmlf("<a href='%s'>", cgit_logo_link); | ||
72 | htmlf("<img id='logo' src='%s'/>\n", cgit_logo); | ||
73 | htmlf("</a>"); | ||
74 | html_txt(title); | ||
75 | html("</div>"); | ||
76 | } | ||