summaryrefslogtreecommitdiffabout
path: root/cgit.c
authorLars Hjemli <hjemli@gmail.com>2006-12-11 15:38:30 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2006-12-11 15:38:30 (UTC)
commit5a106eb09b9b5e189b96cc736046a92b054f6c7f (patch) (unidiff)
tree7172123df870c07deb882a4993fa991b31fc9008 /cgit.c
parent51ada4fda2b47710351e6e4da8a95807d6d9f729 (diff)
downloadcgit-5a106eb09b9b5e189b96cc736046a92b054f6c7f.zip
cgit-5a106eb09b9b5e189b96cc736046a92b054f6c7f.tar.gz
cgit-5a106eb09b9b5e189b96cc736046a92b054f6c7f.tar.bz2
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>
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c94
1 files changed, 14 insertions, 80 deletions
diff --git a/cgit.c b/cgit.c
index 5567859..5438fa1 100644
--- a/cgit.c
+++ b/cgit.c
@@ -10,16 +10,6 @@
10 10
11const char cgit_version[] = CGIT_VERSION; 11const char cgit_version[] = CGIT_VERSION;
12 12
13const 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
17const char cgit_error[] =
18"<div class='error'>%s</div>";
19
20const char cgit_lib_error[] =
21"<div class='error'>%s: %s</div>";
22
23int htmlfd = 0; 13int htmlfd = 0;
24 14
25char *cgit_root = "/usr/src/git"; 15char *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 */ 137static void cgit_print_repolist(struct cacheitem *item)
148static 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
159static int ttl_seconds(int ttl)
160{
161 if (ttl<0)
162 return 60 * 60 * 24 * 365;
163 else
164 return ttl * 60;
165}
166
167static 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
188static void cgit_print_docend()
189{
190 html("</body>\n</html>\n");
191}
192
193static 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
203static 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
405static void cgit_print_repo_page() 339static 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
441static void cgit_refresh_cache(struct cacheitem *item) 375static void cgit_refresh_cache(struct cacheitem *item)