summaryrefslogtreecommitdiffabout
path: root/ui-shared.c
Unidiff
Diffstat (limited to 'ui-shared.c') (more/less context) (ignore whitespace changes)
-rw-r--r--ui-shared.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/ui-shared.c b/ui-shared.c
index 37c60b2..4818e70 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -1,699 +1,707 @@
1/* ui-shared.c: common web output functions 1/* ui-shared.c: common web output functions
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#include "cmd.h" 10#include "cmd.h"
11#include "html.h" 11#include "html.h"
12 12
13const char cgit_doctype[] = 13const 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
17static char *http_date(time_t t) 17static char *http_date(time_t t)
18{ 18{
19 static char day[][4] = 19 static char day[][4] =
20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
21 static char month[][4] = 21 static char month[][4] =
22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
23 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; 23 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
24 struct tm *tm = gmtime(&t); 24 struct tm *tm = gmtime(&t);
25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], 25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
26 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, 26 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
27 tm->tm_hour, tm->tm_min, tm->tm_sec); 27 tm->tm_hour, tm->tm_min, tm->tm_sec);
28} 28}
29 29
30void cgit_print_error(char *msg) 30void cgit_print_error(char *msg)
31{ 31{
32 html("<div class='error'>"); 32 html("<div class='error'>");
33 html_txt(msg); 33 html_txt(msg);
34 html("</div>\n"); 34 html("</div>\n");
35} 35}
36 36
37char *cgit_hosturl() 37char *cgit_hosturl()
38{ 38{
39 char *host, *port; 39 char *host, *port;
40 40
41 host = getenv("SERVER_NAME"); 41 host = getenv("SERVER_NAME");
42 if (!host) 42 if (!host)
43 return NULL; 43 return NULL;
44 port = getenv("SERVER_PORT"); 44 port = getenv("SERVER_PORT");
45 if (port && atoi(port) != 80) 45 if (port && atoi(port) != 80)
46 host = xstrdup(fmt("%s:%d", host, atoi(port))); 46 host = xstrdup(fmt("%s:%d", host, atoi(port)));
47 else 47 else
48 host = xstrdup(host); 48 host = xstrdup(host);
49 return host; 49 return host;
50} 50}
51 51
52char *cgit_rooturl() 52char *cgit_rooturl()
53{ 53{
54 if (ctx.cfg.virtual_root) 54 if (ctx.cfg.virtual_root)
55 return fmt("%s/", ctx.cfg.virtual_root); 55 return fmt("%s/", ctx.cfg.virtual_root);
56 else 56 else
57 return ctx.cfg.script_name; 57 return ctx.cfg.script_name;
58} 58}
59 59
60char *cgit_repourl(const char *reponame) 60char *cgit_repourl(const char *reponame)
61{ 61{
62 if (ctx.cfg.virtual_root) { 62 if (ctx.cfg.virtual_root) {
63 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); 63 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
64 } else { 64 } else {
65 return fmt("?r=%s", reponame); 65 return fmt("?r=%s", reponame);
66 } 66 }
67} 67}
68 68
69char *cgit_fileurl(const char *reponame, const char *pagename, 69char *cgit_fileurl(const char *reponame, const char *pagename,
70 const char *filename, const char *query) 70 const char *filename, const char *query)
71{ 71{
72 char *tmp; 72 char *tmp;
73 char *delim; 73 char *delim;
74 74
75 if (ctx.cfg.virtual_root) { 75 if (ctx.cfg.virtual_root) {
76 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, 76 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
77 pagename, (filename ? filename:"")); 77 pagename, (filename ? filename:""));
78 delim = "?"; 78 delim = "?";
79 } else { 79 } else {
80 tmp = fmt("?url=%s/%s/%s", reponame, pagename, 80 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
81 (filename ? filename : "")); 81 (filename ? filename : ""));
82 delim = "&"; 82 delim = "&";
83 } 83 }
84 if (query) 84 if (query)
85 tmp = fmt("%s%s%s", tmp, delim, query); 85 tmp = fmt("%s%s%s", tmp, delim, query);
86 return tmp; 86 return tmp;
87} 87}
88 88
89char *cgit_pageurl(const char *reponame, const char *pagename, 89char *cgit_pageurl(const char *reponame, const char *pagename,
90 const char *query) 90 const char *query)
91{ 91{
92 return cgit_fileurl(reponame,pagename,0,query); 92 return cgit_fileurl(reponame,pagename,0,query);
93} 93}
94 94
95const char *cgit_repobasename(const char *reponame) 95const char *cgit_repobasename(const char *reponame)
96{ 96{
97 /* I assume we don't need to store more than one repo basename */ 97 /* I assume we don't need to store more than one repo basename */
98 static char rvbuf[1024]; 98 static char rvbuf[1024];
99 int p; 99 int p;
100 const char *rv; 100 const char *rv;
101 strncpy(rvbuf,reponame,sizeof(rvbuf)); 101 strncpy(rvbuf,reponame,sizeof(rvbuf));
102 if(rvbuf[sizeof(rvbuf)-1]) 102 if(rvbuf[sizeof(rvbuf)-1])
103 die("cgit_repobasename: truncated repository name '%s'", reponame); 103 die("cgit_repobasename: truncated repository name '%s'", reponame);
104 p = strlen(rvbuf)-1; 104 p = strlen(rvbuf)-1;
105 /* strip trailing slashes */ 105 /* strip trailing slashes */
106 while(p && rvbuf[p]=='/') rvbuf[p--]=0; 106 while(p && rvbuf[p]=='/') rvbuf[p--]=0;
107 /* strip trailing .git */ 107 /* strip trailing .git */
108 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { 108 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
109 p -= 3; rvbuf[p--] = 0; 109 p -= 3; rvbuf[p--] = 0;
110 } 110 }
111 /* strip more trailing slashes if any */ 111 /* strip more trailing slashes if any */
112 while( p && rvbuf[p]=='/') rvbuf[p--]=0; 112 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
113 /* find last slash in the remaining string */ 113 /* find last slash in the remaining string */
114 rv = strrchr(rvbuf,'/'); 114 rv = strrchr(rvbuf,'/');
115 if(rv) 115 if(rv)
116 return ++rv; 116 return ++rv;
117 return rvbuf; 117 return rvbuf;
118} 118}
119 119
120char *cgit_currurl() 120char *cgit_currurl()
121{ 121{
122 if (!ctx.cfg.virtual_root) 122 if (!ctx.cfg.virtual_root)
123 return ctx.cfg.script_name; 123 return ctx.cfg.script_name;
124 else if (ctx.qry.page) 124 else if (ctx.qry.page)
125 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); 125 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
126 else if (ctx.qry.repo) 126 else if (ctx.qry.repo)
127 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); 127 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
128 else 128 else
129 return fmt("%s/", ctx.cfg.virtual_root); 129 return fmt("%s/", ctx.cfg.virtual_root);
130} 130}
131 131
132static void site_url(char *page, char *search, int ofs) 132static void site_url(char *page, char *search, int ofs)
133{ 133{
134 char *delim = "?"; 134 char *delim = "?";
135 135
136 if (ctx.cfg.virtual_root) { 136 if (ctx.cfg.virtual_root) {
137 html_attr(ctx.cfg.virtual_root); 137 html_attr(ctx.cfg.virtual_root);
138 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') 138 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
139 html("/"); 139 html("/");
140 } else 140 } else
141 html(ctx.cfg.script_name); 141 html(ctx.cfg.script_name);
142 142
143 if (page) { 143 if (page) {
144 htmlf("?p=%s", page); 144 htmlf("?p=%s", page);
145 delim = "&"; 145 delim = "&";
146 } 146 }
147 if (search) { 147 if (search) {
148 html(delim); 148 html(delim);
149 html("q="); 149 html("q=");
150 html_attr(search); 150 html_attr(search);
151 delim = "&"; 151 delim = "&";
152 } 152 }
153 if (ofs) { 153 if (ofs) {
154 html(delim); 154 html(delim);
155 htmlf("ofs=%d", ofs); 155 htmlf("ofs=%d", ofs);
156 } 156 }
157} 157}
158 158
159static void site_link(char *page, char *name, char *title, char *class, 159static void site_link(char *page, char *name, char *title, char *class,
160 char *search, int ofs) 160 char *search, int ofs)
161{ 161{
162 html("<a"); 162 html("<a");
163 if (title) { 163 if (title) {
164 html(" title='"); 164 html(" title='");
165 html_attr(title); 165 html_attr(title);
166 html("'"); 166 html("'");
167 } 167 }
168 if (class) { 168 if (class) {
169 html(" class='"); 169 html(" class='");
170 html_attr(class); 170 html_attr(class);
171 html("'"); 171 html("'");
172 } 172 }
173 html(" href='"); 173 html(" href='");
174 site_url(page, search, ofs); 174 site_url(page, search, ofs);
175 html("'>"); 175 html("'>");
176 html_txt(name); 176 html_txt(name);
177 html("</a>"); 177 html("</a>");
178} 178}
179 179
180void cgit_index_link(char *name, char *title, char *class, char *pattern, 180void cgit_index_link(char *name, char *title, char *class, char *pattern,
181 int ofs) 181 int ofs)
182{ 182{
183 site_link(NULL, name, title, class, pattern, ofs); 183 site_link(NULL, name, title, class, pattern, ofs);
184} 184}
185 185
186static char *repolink(char *title, char *class, char *page, char *head, 186static char *repolink(char *title, char *class, char *page, char *head,
187 char *path) 187 char *path)
188{ 188{
189 char *delim = "?"; 189 char *delim = "?";
190 190
191 html("<a"); 191 html("<a");
192 if (title) { 192 if (title) {
193 html(" title='"); 193 html(" title='");
194 html_attr(title); 194 html_attr(title);
195 html("'"); 195 html("'");
196 } 196 }
197 if (class) { 197 if (class) {
198 html(" class='"); 198 html(" class='");
199 html_attr(class); 199 html_attr(class);
200 html("'"); 200 html("'");
201 } 201 }
202 html(" href='"); 202 html(" href='");
203 if (ctx.cfg.virtual_root) { 203 if (ctx.cfg.virtual_root) {
204 html_attr(ctx.cfg.virtual_root); 204 html_attr(ctx.cfg.virtual_root);
205 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') 205 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
206 html("/"); 206 html("/");
207 html_attr(ctx.repo->url); 207 html_attr(ctx.repo->url);
208 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 208 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
209 html("/"); 209 html("/");
210 if (page) { 210 if (page) {
211 html(page); 211 html(page);
212 html("/"); 212 html("/");
213 if (path) 213 if (path)
214 html_attr(path); 214 html_attr(path);
215 } 215 }
216 } else { 216 } else {
217 html(ctx.cfg.script_name); 217 html(ctx.cfg.script_name);
218 html("?url="); 218 html("?url=");
219 html_attr(ctx.repo->url); 219 html_attr(ctx.repo->url);
220 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 220 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
221 html("/"); 221 html("/");
222 if (page) { 222 if (page) {
223 html(page); 223 html(page);
224 html("/"); 224 html("/");
225 if (path) 225 if (path)
226 html_attr(path); 226 html_attr(path);
227 } 227 }
228 delim = "&amp;"; 228 delim = "&amp;";
229 } 229 }
230 if (head && strcmp(head, ctx.repo->defbranch)) { 230 if (head && strcmp(head, ctx.repo->defbranch)) {
231 html(delim); 231 html(delim);
232 html("h="); 232 html("h=");
233 html_attr(head); 233 html_attr(head);
234 delim = "&amp;"; 234 delim = "&amp;";
235 } 235 }
236 return fmt("%s", delim); 236 return fmt("%s", delim);
237} 237}
238 238
239static void reporevlink(char *page, char *name, char *title, char *class, 239static void reporevlink(char *page, char *name, char *title, char *class,
240 char *head, char *rev, char *path) 240 char *head, char *rev, char *path)
241{ 241{
242 char *delim; 242 char *delim;
243 243
244 delim = repolink(title, class, page, head, path); 244 delim = repolink(title, class, page, head, path);
245 if (rev && strcmp(rev, ctx.qry.head)) { 245 if (rev && strcmp(rev, ctx.qry.head)) {
246 html(delim); 246 html(delim);
247 html("id="); 247 html("id=");
248 html_attr(rev); 248 html_attr(rev);
249 } 249 }
250 html("'>"); 250 html("'>");
251 html_txt(name); 251 html_txt(name);
252 html("</a>"); 252 html("</a>");
253} 253}
254 254
255void cgit_tree_link(char *name, char *title, char *class, char *head, 255void cgit_tree_link(char *name, char *title, char *class, char *head,
256 char *rev, char *path) 256 char *rev, char *path)
257{ 257{
258 reporevlink("tree", name, title, class, head, rev, path); 258 reporevlink("tree", name, title, class, head, rev, path);
259} 259}
260 260
261void cgit_plain_link(char *name, char *title, char *class, char *head,
262 char *rev, char *path)
263{
264 reporevlink("plain", name, title, class, head, rev, path);
265}
266
261void cgit_log_link(char *name, char *title, char *class, char *head, 267void cgit_log_link(char *name, char *title, char *class, char *head,
262 char *rev, char *path, int ofs, char *grep, char *pattern) 268 char *rev, char *path, int ofs, char *grep, char *pattern)
263{ 269{
264 char *delim; 270 char *delim;
265 271
266 delim = repolink(title, class, "log", head, path); 272 delim = repolink(title, class, "log", head, path);
267 if (rev && strcmp(rev, ctx.qry.head)) { 273 if (rev && strcmp(rev, ctx.qry.head)) {
268 html(delim); 274 html(delim);
269 html("id="); 275 html("id=");
270 html_attr(rev); 276 html_attr(rev);
271 delim = "&"; 277 delim = "&";
272 } 278 }
273 if (grep && pattern) { 279 if (grep && pattern) {
274 html(delim); 280 html(delim);
275 html("qt="); 281 html("qt=");
276 html_attr(grep); 282 html_attr(grep);
277 delim = "&"; 283 delim = "&";
278 html(delim); 284 html(delim);
279 html("q="); 285 html("q=");
280 html_attr(pattern); 286 html_attr(pattern);
281 } 287 }
282 if (ofs > 0) { 288 if (ofs > 0) {
283 html(delim); 289 html(delim);
284 html("ofs="); 290 html("ofs=");
285 htmlf("%d", ofs); 291 htmlf("%d", ofs);
286 } 292 }
287 html("'>"); 293 html("'>");
288 html_txt(name); 294 html_txt(name);
289 html("</a>"); 295 html("</a>");
290} 296}
291 297
292void cgit_commit_link(char *name, char *title, char *class, char *head, 298void cgit_commit_link(char *name, char *title, char *class, char *head,
293 char *rev) 299 char *rev)
294{ 300{
295 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) { 301 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
296 name[ctx.cfg.max_msg_len] = '\0'; 302 name[ctx.cfg.max_msg_len] = '\0';
297 name[ctx.cfg.max_msg_len - 1] = '.'; 303 name[ctx.cfg.max_msg_len - 1] = '.';
298 name[ctx.cfg.max_msg_len - 2] = '.'; 304 name[ctx.cfg.max_msg_len - 2] = '.';
299 name[ctx.cfg.max_msg_len - 3] = '.'; 305 name[ctx.cfg.max_msg_len - 3] = '.';
300 } 306 }
301 reporevlink("commit", name, title, class, head, rev, NULL); 307 reporevlink("commit", name, title, class, head, rev, NULL);
302} 308}
303 309
304void cgit_refs_link(char *name, char *title, char *class, char *head, 310void cgit_refs_link(char *name, char *title, char *class, char *head,
305 char *rev, char *path) 311 char *rev, char *path)
306{ 312{
307 reporevlink("refs", name, title, class, head, rev, path); 313 reporevlink("refs", name, title, class, head, rev, path);
308} 314}
309 315
310void cgit_snapshot_link(char *name, char *title, char *class, char *head, 316void cgit_snapshot_link(char *name, char *title, char *class, char *head,
311 char *rev, char *archivename) 317 char *rev, char *archivename)
312{ 318{
313 reporevlink("snapshot", name, title, class, head, rev, archivename); 319 reporevlink("snapshot", name, title, class, head, rev, archivename);
314} 320}
315 321
316void cgit_diff_link(char *name, char *title, char *class, char *head, 322void cgit_diff_link(char *name, char *title, char *class, char *head,
317 char *new_rev, char *old_rev, char *path) 323 char *new_rev, char *old_rev, char *path)
318{ 324{
319 char *delim; 325 char *delim;
320 326
321 delim = repolink(title, class, "diff", head, path); 327 delim = repolink(title, class, "diff", head, path);
322 if (new_rev && strcmp(new_rev, ctx.qry.head)) { 328 if (new_rev && strcmp(new_rev, ctx.qry.head)) {
323 html(delim); 329 html(delim);
324 html("id="); 330 html("id=");
325 html_attr(new_rev); 331 html_attr(new_rev);
326 delim = "&amp;"; 332 delim = "&amp;";
327 } 333 }
328 if (old_rev) { 334 if (old_rev) {
329 html(delim); 335 html(delim);
330 html("id2="); 336 html("id2=");
331 html_attr(old_rev); 337 html_attr(old_rev);
332 } 338 }
333 html("'>"); 339 html("'>");
334 html_txt(name); 340 html_txt(name);
335 html("</a>"); 341 html("</a>");
336} 342}
337 343
338void cgit_patch_link(char *name, char *title, char *class, char *head, 344void cgit_patch_link(char *name, char *title, char *class, char *head,
339 char *rev) 345 char *rev)
340{ 346{
341 reporevlink("patch", name, title, class, head, rev, NULL); 347 reporevlink("patch", name, title, class, head, rev, NULL);
342} 348}
343 349
344void cgit_object_link(struct object *obj) 350void cgit_object_link(struct object *obj)
345{ 351{
346 char *page, *arg, *url; 352 char *page, *arg, *url;
347 353
348 if (obj->type == OBJ_COMMIT) { 354 if (obj->type == OBJ_COMMIT) {
349 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, 355 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
350 ctx.qry.head, sha1_to_hex(obj->sha1)); 356 ctx.qry.head, sha1_to_hex(obj->sha1));
351 return; 357 return;
352 } else if (obj->type == OBJ_TREE) { 358 } else if (obj->type == OBJ_TREE) {
353 page = "tree"; 359 page = "tree";
354 arg = "id"; 360 arg = "id";
355 } else if (obj->type == OBJ_TAG) { 361 } else if (obj->type == OBJ_TAG) {
356 page = "tag"; 362 page = "tag";
357 arg = "id"; 363 arg = "id";
358 } else { 364 } else {
359 page = "blob"; 365 page = "blob";
360 arg = "id"; 366 arg = "id";
361 } 367 }
362 368
363 url = cgit_pageurl(ctx.qry.repo, page, 369 url = cgit_pageurl(ctx.qry.repo, page,
364 fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); 370 fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
365 html_link_open(url, NULL, NULL); 371 html_link_open(url, NULL, NULL);
366 htmlf("%s %s", typename(obj->type), 372 htmlf("%s %s", typename(obj->type),
367 sha1_to_hex(obj->sha1)); 373 sha1_to_hex(obj->sha1));
368 html_link_close(); 374 html_link_close();
369} 375}
370 376
371void cgit_print_date(time_t secs, char *format, int local_time) 377void cgit_print_date(time_t secs, char *format, int local_time)
372{ 378{
373 char buf[64]; 379 char buf[64];
374 struct tm *time; 380 struct tm *time;
375 381
376 if (!secs) 382 if (!secs)
377 return; 383 return;
378 if(local_time) 384 if(local_time)
379 time = localtime(&secs); 385 time = localtime(&secs);
380 else 386 else
381 time = gmtime(&secs); 387 time = gmtime(&secs);
382 strftime(buf, sizeof(buf)-1, format, time); 388 strftime(buf, sizeof(buf)-1, format, time);
383 html_txt(buf); 389 html_txt(buf);
384} 390}
385 391
386void cgit_print_age(time_t t, time_t max_relative, char *format) 392void cgit_print_age(time_t t, time_t max_relative, char *format)
387{ 393{
388 time_t now, secs; 394 time_t now, secs;
389 395
390 if (!t) 396 if (!t)
391 return; 397 return;
392 time(&now); 398 time(&now);
393 secs = now - t; 399 secs = now - t;
394 400
395 if (secs > max_relative && max_relative >= 0) { 401 if (secs > max_relative && max_relative >= 0) {
396 cgit_print_date(t, format, ctx.cfg.local_time); 402 cgit_print_date(t, format, ctx.cfg.local_time);
397 return; 403 return;
398 } 404 }
399 405
400 if (secs < TM_HOUR * 2) { 406 if (secs < TM_HOUR * 2) {
401 htmlf("<span class='age-mins'>%.0f min.</span>", 407 htmlf("<span class='age-mins'>%.0f min.</span>",
402 secs * 1.0 / TM_MIN); 408 secs * 1.0 / TM_MIN);
403 return; 409 return;
404 } 410 }
405 if (secs < TM_DAY * 2) { 411 if (secs < TM_DAY * 2) {
406 htmlf("<span class='age-hours'>%.0f hours</span>", 412 htmlf("<span class='age-hours'>%.0f hours</span>",
407 secs * 1.0 / TM_HOUR); 413 secs * 1.0 / TM_HOUR);
408 return; 414 return;
409 } 415 }
410 if (secs < TM_WEEK * 2) { 416 if (secs < TM_WEEK * 2) {
411 htmlf("<span class='age-days'>%.0f days</span>", 417 htmlf("<span class='age-days'>%.0f days</span>",
412 secs * 1.0 / TM_DAY); 418 secs * 1.0 / TM_DAY);
413 return; 419 return;
414 } 420 }
415 if (secs < TM_MONTH * 2) { 421 if (secs < TM_MONTH * 2) {
416 htmlf("<span class='age-weeks'>%.0f weeks</span>", 422 htmlf("<span class='age-weeks'>%.0f weeks</span>",
417 secs * 1.0 / TM_WEEK); 423 secs * 1.0 / TM_WEEK);
418 return; 424 return;
419 } 425 }
420 if (secs < TM_YEAR * 2) { 426 if (secs < TM_YEAR * 2) {
421 htmlf("<span class='age-months'>%.0f months</span>", 427 htmlf("<span class='age-months'>%.0f months</span>",
422 secs * 1.0 / TM_MONTH); 428 secs * 1.0 / TM_MONTH);
423 return; 429 return;
424 } 430 }
425 htmlf("<span class='age-years'>%.0f years</span>", 431 htmlf("<span class='age-years'>%.0f years</span>",
426 secs * 1.0 / TM_YEAR); 432 secs * 1.0 / TM_YEAR);
427} 433}
428 434
429void cgit_print_http_headers(struct cgit_context *ctx) 435void cgit_print_http_headers(struct cgit_context *ctx)
430{ 436{
431 if (ctx->page.mimetype && ctx->page.charset) 437 if (ctx->page.mimetype && ctx->page.charset)
432 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, 438 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
433 ctx->page.charset); 439 ctx->page.charset);
434 else if (ctx->page.mimetype) 440 else if (ctx->page.mimetype)
435 htmlf("Content-Type: %s\n", ctx->page.mimetype); 441 htmlf("Content-Type: %s\n", ctx->page.mimetype);
442 if (ctx->page.size)
443 htmlf("Content-Length: %ld\n", ctx->page.size);
436 if (ctx->page.filename) 444 if (ctx->page.filename)
437 htmlf("Content-Disposition: inline; filename=\"%s\"\n", 445 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
438 ctx->page.filename); 446 ctx->page.filename);
439 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); 447 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
440 htmlf("Expires: %s\n", http_date(ctx->page.expires)); 448 htmlf("Expires: %s\n", http_date(ctx->page.expires));
441 html("\n"); 449 html("\n");
442} 450}
443 451
444void cgit_print_docstart(struct cgit_context *ctx) 452void cgit_print_docstart(struct cgit_context *ctx)
445{ 453{
446 char *host = cgit_hosturl(); 454 char *host = cgit_hosturl();
447 html(cgit_doctype); 455 html(cgit_doctype);
448 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 456 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
449 html("<head>\n"); 457 html("<head>\n");
450 html("<title>"); 458 html("<title>");
451 html_txt(ctx->page.title); 459 html_txt(ctx->page.title);
452 html("</title>\n"); 460 html("</title>\n");
453 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 461 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
454 if (ctx->cfg.robots && *ctx->cfg.robots) 462 if (ctx->cfg.robots && *ctx->cfg.robots)
455 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); 463 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
456 html("<link rel='stylesheet' type='text/css' href='"); 464 html("<link rel='stylesheet' type='text/css' href='");
457 html_attr(ctx->cfg.css); 465 html_attr(ctx->cfg.css);
458 html("'/>\n"); 466 html("'/>\n");
459 if (ctx->cfg.favicon) { 467 if (ctx->cfg.favicon) {
460 html("<link rel='shortcut icon' href='"); 468 html("<link rel='shortcut icon' href='");
461 html_attr(ctx->cfg.favicon); 469 html_attr(ctx->cfg.favicon);
462 html("'/>\n"); 470 html("'/>\n");
463 } 471 }
464 if (host && ctx->repo) { 472 if (host && ctx->repo) {
465 html("<link rel='alternate' title='Atom feed' href='http://"); 473 html("<link rel='alternate' title='Atom feed' href='http://");
466 html_attr(cgit_hosturl()); 474 html_attr(cgit_hosturl());
467 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path, 475 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path,
468 fmt("h=%s", ctx->qry.head))); 476 fmt("h=%s", ctx->qry.head)));
469 html("' type='application/atom+xml'/>"); 477 html("' type='application/atom+xml'/>");
470 } 478 }
471 html("</head>\n"); 479 html("</head>\n");
472 html("<body>\n"); 480 html("<body>\n");
473} 481}
474 482
475void cgit_print_docend() 483void cgit_print_docend()
476{ 484{
477 html("</div>"); 485 html("</div>");
478 if (ctx.cfg.footer) 486 if (ctx.cfg.footer)
479 html_include(ctx.cfg.footer); 487 html_include(ctx.cfg.footer);
480 else { 488 else {
481 html("<div class='footer'>generated "); 489 html("<div class='footer'>generated ");
482 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time); 490 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
483 htmlf(" by cgit %s", cgit_version); 491 htmlf(" by cgit %s", cgit_version);
484 html("</div>\n"); 492 html("</div>\n");
485 } 493 }
486 html("</body>\n</html>\n"); 494 html("</body>\n</html>\n");
487} 495}
488 496
489int print_branch_option(const char *refname, const unsigned char *sha1, 497int print_branch_option(const char *refname, const unsigned char *sha1,
490 int flags, void *cb_data) 498 int flags, void *cb_data)
491{ 499{
492 char *name = (char *)refname; 500 char *name = (char *)refname;
493 html_option(name, name, ctx.qry.head); 501 html_option(name, name, ctx.qry.head);
494 return 0; 502 return 0;
495} 503}
496 504
497int print_archive_ref(const char *refname, const unsigned char *sha1, 505int print_archive_ref(const char *refname, const unsigned char *sha1,
498 int flags, void *cb_data) 506 int flags, void *cb_data)
499{ 507{
500 struct tag *tag; 508 struct tag *tag;
501 struct taginfo *info; 509 struct taginfo *info;
502 struct object *obj; 510 struct object *obj;
503 char buf[256], *url; 511 char buf[256], *url;
504 unsigned char fileid[20]; 512 unsigned char fileid[20];
505 int *header = (int *)cb_data; 513 int *header = (int *)cb_data;
506 514
507 if (prefixcmp(refname, "refs/archives")) 515 if (prefixcmp(refname, "refs/archives"))
508 return 0; 516 return 0;
509 strncpy(buf, refname+14, sizeof(buf)); 517 strncpy(buf, refname+14, sizeof(buf));
510 obj = parse_object(sha1); 518 obj = parse_object(sha1);
511 if (!obj) 519 if (!obj)
512 return 1; 520 return 1;
513 if (obj->type == OBJ_TAG) { 521 if (obj->type == OBJ_TAG) {
514 tag = lookup_tag(sha1); 522 tag = lookup_tag(sha1);
515 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) 523 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
516 return 0; 524 return 0;
517 hashcpy(fileid, tag->tagged->sha1); 525 hashcpy(fileid, tag->tagged->sha1);
518 } else if (obj->type != OBJ_BLOB) { 526 } else if (obj->type != OBJ_BLOB) {
519 return 0; 527 return 0;
520 } else { 528 } else {
521 hashcpy(fileid, sha1); 529 hashcpy(fileid, sha1);
522 } 530 }
523 if (!*header) { 531 if (!*header) {
524 html("<h1>download</h1>\n"); 532 html("<h1>download</h1>\n");
525 *header = 1; 533 *header = 1;
526 } 534 }
527 url = cgit_pageurl(ctx.qry.repo, "blob", 535 url = cgit_pageurl(ctx.qry.repo, "blob",
528 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid), 536 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
529 buf)); 537 buf));
530 html_link_open(url, NULL, "menu"); 538 html_link_open(url, NULL, "menu");
531 html_txt(strlpart(buf, 20)); 539 html_txt(strlpart(buf, 20));
532 html_link_close(); 540 html_link_close();
533 return 0; 541 return 0;
534} 542}
535 543
536void add_hidden_formfields(int incl_head, int incl_search, char *page) 544void add_hidden_formfields(int incl_head, int incl_search, char *page)
537{ 545{
538 char *url; 546 char *url;
539 547
540 if (!ctx.cfg.virtual_root) { 548 if (!ctx.cfg.virtual_root) {
541 url = fmt("%s/%s", ctx.qry.repo, page); 549 url = fmt("%s/%s", ctx.qry.repo, page);
542 if (ctx.qry.path) 550 if (ctx.qry.path)
543 url = fmt("%s/%s", url, ctx.qry.path); 551 url = fmt("%s/%s", url, ctx.qry.path);
544 html_hidden("url", url); 552 html_hidden("url", url);
545 } 553 }
546 554
547 if (incl_head && ctx.qry.head && ctx.repo->defbranch && 555 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
548 strcmp(ctx.qry.head, ctx.repo->defbranch)) 556 strcmp(ctx.qry.head, ctx.repo->defbranch))
549 html_hidden("h", ctx.qry.head); 557 html_hidden("h", ctx.qry.head);
550 558
551 if (ctx.qry.sha1) 559 if (ctx.qry.sha1)
552 html_hidden("id", ctx.qry.sha1); 560 html_hidden("id", ctx.qry.sha1);
553 if (ctx.qry.sha2) 561 if (ctx.qry.sha2)
554 html_hidden("id2", ctx.qry.sha2); 562 html_hidden("id2", ctx.qry.sha2);
555 563
556 if (incl_search) { 564 if (incl_search) {
557 if (ctx.qry.grep) 565 if (ctx.qry.grep)
558 html_hidden("qt", ctx.qry.grep); 566 html_hidden("qt", ctx.qry.grep);
559 if (ctx.qry.search) 567 if (ctx.qry.search)
560 html_hidden("q", ctx.qry.search); 568 html_hidden("q", ctx.qry.search);
561 } 569 }
562} 570}
563 571
564char *hc(struct cgit_cmd *cmd, const char *page) 572char *hc(struct cgit_cmd *cmd, const char *page)
565{ 573{
566 return (strcmp(cmd->name, page) ? NULL : "active"); 574 return (strcmp(cmd->name, page) ? NULL : "active");
567} 575}
568 576
569void cgit_print_pageheader(struct cgit_context *ctx) 577void cgit_print_pageheader(struct cgit_context *ctx)
570{ 578{
571 struct cgit_cmd *cmd = cgit_get_cmd(ctx); 579 struct cgit_cmd *cmd = cgit_get_cmd(ctx);
572 580
573 html("<table id='header'>\n"); 581 html("<table id='header'>\n");
574 html("<tr>\n"); 582 html("<tr>\n");
575 html("<td class='logo' rowspan='2'><a href='"); 583 html("<td class='logo' rowspan='2'><a href='");
576 if (ctx->cfg.logo_link) 584 if (ctx->cfg.logo_link)
577 html_attr(ctx->cfg.logo_link); 585 html_attr(ctx->cfg.logo_link);
578 else 586 else
579 html_attr(cgit_rooturl()); 587 html_attr(cgit_rooturl());
580 html("'><img src='"); 588 html("'><img src='");
581 html_attr(ctx->cfg.logo); 589 html_attr(ctx->cfg.logo);
582 html("' alt='cgit logo'/></a></td>\n"); 590 html("' alt='cgit logo'/></a></td>\n");
583 591
584 html("<td class='main'>"); 592 html("<td class='main'>");
585 if (ctx->repo) { 593 if (ctx->repo) {
586 cgit_index_link("index", NULL, NULL, NULL, 0); 594 cgit_index_link("index", NULL, NULL, NULL, 0);
587 html(" : "); 595 html(" : ");
588 reporevlink(NULL, ctx->repo->name, NULL, hc(cmd, "summary"), 596 reporevlink(NULL, ctx->repo->name, NULL, hc(cmd, "summary"),
589 ctx->qry.head, NULL, NULL); 597 ctx->qry.head, NULL, NULL);
590 html("</td><td class='form'>"); 598 html("</td><td class='form'>");
591 html("<form method='get' action=''>\n"); 599 html("<form method='get' action=''>\n");
592 add_hidden_formfields(0, 1, ctx->qry.page); 600 add_hidden_formfields(0, 1, ctx->qry.page);
593 html("<select name='h' onchange='this.form.submit();'>\n"); 601 html("<select name='h' onchange='this.form.submit();'>\n");
594 for_each_branch_ref(print_branch_option, ctx->qry.head); 602 for_each_branch_ref(print_branch_option, ctx->qry.head);
595 html("</select> "); 603 html("</select> ");
596 html("<input type='submit' name='' value='switch'/>"); 604 html("<input type='submit' name='' value='switch'/>");
597 html("</form>"); 605 html("</form>");
598 } else 606 } else
599 html_txt(ctx->cfg.root_title); 607 html_txt(ctx->cfg.root_title);
600 html("</td></tr>\n"); 608 html("</td></tr>\n");
601 609
602 html("<tr><td class='sub'>"); 610 html("<tr><td class='sub'>");
603 if (ctx->repo) { 611 if (ctx->repo) {
604 html_txt(ctx->repo->desc); 612 html_txt(ctx->repo->desc);
605 html("</td><td class='sub right'>"); 613 html("</td><td class='sub right'>");
606 html_txt(ctx->repo->owner); 614 html_txt(ctx->repo->owner);
607 } else { 615 } else {
608 if (ctx->cfg.root_desc) 616 if (ctx->cfg.root_desc)
609 html_txt(ctx->cfg.root_desc); 617 html_txt(ctx->cfg.root_desc);
610 else if (ctx->cfg.index_info) 618 else if (ctx->cfg.index_info)
611 html_include(ctx->cfg.index_info); 619 html_include(ctx->cfg.index_info);
612 } 620 }
613 html("</td></tr></table>\n"); 621 html("</td></tr></table>\n");
614 622
615 html("<table class='tabs'><tr><td>\n"); 623 html("<table class='tabs'><tr><td>\n");
616 if (ctx->repo) { 624 if (ctx->repo) {
617 reporevlink(NULL, "summary", NULL, hc(cmd, "summary"), 625 reporevlink(NULL, "summary", NULL, hc(cmd, "summary"),
618 ctx->qry.head, NULL, NULL); 626 ctx->qry.head, NULL, NULL);
619 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head, 627 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head,
620 ctx->qry.sha1, NULL); 628 ctx->qry.sha1, NULL);
621 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head, 629 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head,
622 NULL, NULL, 0, NULL, NULL); 630 NULL, NULL, 0, NULL, NULL);
623 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head, 631 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head,
624 ctx->qry.sha1, NULL); 632 ctx->qry.sha1, NULL);
625 cgit_commit_link("commit", NULL, hc(cmd, "commit"), 633 cgit_commit_link("commit", NULL, hc(cmd, "commit"),
626 ctx->qry.head, ctx->qry.sha1); 634 ctx->qry.head, ctx->qry.sha1);
627 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head, 635 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head,
628 ctx->qry.sha1, ctx->qry.sha2, NULL); 636 ctx->qry.sha1, ctx->qry.sha2, NULL);
629 if (ctx->repo->readme) 637 if (ctx->repo->readme)
630 reporevlink("about", "about", NULL, 638 reporevlink("about", "about", NULL,
631 hc(cmd, "about"), ctx->qry.head, NULL, 639 hc(cmd, "about"), ctx->qry.head, NULL,
632 NULL); 640 NULL);
633 html("</td><td class='form'>"); 641 html("</td><td class='form'>");
634 html("<form class='right' method='get' action='"); 642 html("<form class='right' method='get' action='");
635 if (ctx->cfg.virtual_root) 643 if (ctx->cfg.virtual_root)
636 html_attr(cgit_fileurl(ctx->qry.repo, "log", 644 html_attr(cgit_fileurl(ctx->qry.repo, "log",
637 ctx->qry.path, NULL)); 645 ctx->qry.path, NULL));
638 html("'>\n"); 646 html("'>\n");
639 add_hidden_formfields(1, 0, "log"); 647 add_hidden_formfields(1, 0, "log");
640 html("<select name='qt'>\n"); 648 html("<select name='qt'>\n");
641 html_option("grep", "log msg", ctx->qry.grep); 649 html_option("grep", "log msg", ctx->qry.grep);
642 html_option("author", "author", ctx->qry.grep); 650 html_option("author", "author", ctx->qry.grep);
643 html_option("committer", "committer", ctx->qry.grep); 651 html_option("committer", "committer", ctx->qry.grep);
644 html("</select>\n"); 652 html("</select>\n");
645 html("<input class='txt' type='text' size='10' name='q' value='"); 653 html("<input class='txt' type='text' size='10' name='q' value='");
646 html_attr(ctx->qry.search); 654 html_attr(ctx->qry.search);
647 html("'/>\n"); 655 html("'/>\n");
648 html("<input type='submit' value='search'/>\n"); 656 html("<input type='submit' value='search'/>\n");
649 html("</form>\n"); 657 html("</form>\n");
650 } else { 658 } else {
651 site_link(NULL, "index", NULL, hc(cmd, "repolist"), NULL, 0); 659 site_link(NULL, "index", NULL, hc(cmd, "repolist"), NULL, 0);
652 if (ctx->cfg.root_readme) 660 if (ctx->cfg.root_readme)
653 site_link("about", "about", NULL, hc(cmd, "about"), 661 site_link("about", "about", NULL, hc(cmd, "about"),
654 NULL, 0); 662 NULL, 0);
655 html("</td><td class='form'>"); 663 html("</td><td class='form'>");
656 html("<form method='get' action='"); 664 html("<form method='get' action='");
657 html_attr(cgit_rooturl()); 665 html_attr(cgit_rooturl());
658 html("'>\n"); 666 html("'>\n");
659 html("<input type='text' name='q' size='10' value='"); 667 html("<input type='text' name='q' size='10' value='");
660 html_attr(ctx->qry.search); 668 html_attr(ctx->qry.search);
661 html("'/>\n"); 669 html("'/>\n");
662 html("<input type='submit' value='search'/>\n"); 670 html("<input type='submit' value='search'/>\n");
663 html("</form>"); 671 html("</form>");
664 } 672 }
665 html("</td></tr></table>\n"); 673 html("</td></tr></table>\n");
666 html("<div class='content'>"); 674 html("<div class='content'>");
667} 675}
668 676
669void cgit_print_filemode(unsigned short mode) 677void cgit_print_filemode(unsigned short mode)
670{ 678{
671 if (S_ISDIR(mode)) 679 if (S_ISDIR(mode))
672 html("d"); 680 html("d");
673 else if (S_ISLNK(mode)) 681 else if (S_ISLNK(mode))
674 html("l"); 682 html("l");
675 else if (S_ISGITLINK(mode)) 683 else if (S_ISGITLINK(mode))
676 html("m"); 684 html("m");
677 else 685 else
678 html("-"); 686 html("-");
679 html_fileperm(mode >> 6); 687 html_fileperm(mode >> 6);
680 html_fileperm(mode >> 3); 688 html_fileperm(mode >> 3);
681 html_fileperm(mode); 689 html_fileperm(mode);
682} 690}
683 691
684void cgit_print_snapshot_links(const char *repo, const char *head, 692void cgit_print_snapshot_links(const char *repo, const char *head,
685 const char *hex, int snapshots) 693 const char *hex, int snapshots)
686{ 694{
687 const struct cgit_snapshot_format* f; 695 const struct cgit_snapshot_format* f;
688 char *filename; 696 char *filename;
689 697
690 for (f = cgit_snapshot_formats; f->suffix; f++) { 698 for (f = cgit_snapshot_formats; f->suffix; f++) {
691 if (!(snapshots & f->bit)) 699 if (!(snapshots & f->bit))
692 continue; 700 continue;
693 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, 701 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
694 f->suffix); 702 f->suffix);
695 cgit_snapshot_link(filename, NULL, NULL, (char *)head, 703 cgit_snapshot_link(filename, NULL, NULL, (char *)head,
696 (char *)hex, filename); 704 (char *)hex, filename);
697 html("<br/>"); 705 html("<br/>");
698 } 706 }
699} 707}