]> gitweb.ps.run Git - ps-cgit/blob - ui-shared.c
about: always ensure page has a trailing slash
[ps-cgit] / ui-shared.c
1 /* ui-shared.c: common web output functions
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10 #include "ui-shared.h"
11 #include "cmd.h"
12 #include "html.h"
13
14 static const char cgit_doctype[] =
15 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
16 "  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
17
18 static char *http_date(time_t t)
19 {
20         static char day[][4] =
21                 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
22         static char month[][4] =
23                 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
24                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
25         struct tm *tm = gmtime(&t);
26         return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
27                    tm->tm_mday, month[tm->tm_mon], 1900 + tm->tm_year,
28                    tm->tm_hour, tm->tm_min, tm->tm_sec);
29 }
30
31 void cgit_print_error(const char *fmt, ...)
32 {
33         va_list ap;
34         va_start(ap, fmt);
35         cgit_vprint_error(fmt, ap);
36         va_end(ap);
37 }
38
39 void cgit_vprint_error(const char *fmt, va_list ap)
40 {
41         va_list cp;
42         html("<div class='error'>");
43         va_copy(cp, ap);
44         html_vtxtf(fmt, cp);
45         va_end(cp);
46         html("</div>\n");
47 }
48
49 const char *cgit_httpscheme(void)
50 {
51         if (ctx.env.https && !strcmp(ctx.env.https, "on"))
52                 return "https://";
53         else
54                 return "http://";
55 }
56
57 const char *cgit_hosturl(void)
58 {
59         if (ctx.env.http_host)
60                 return ctx.env.http_host;
61         if (!ctx.env.server_name)
62                 return NULL;
63         if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
64                 return ctx.env.server_name;
65         return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
66 }
67
68 const char *cgit_currenturl(void)
69 {
70         if (!ctx.qry.url)
71                 return cgit_rooturl();
72         const char *root = cgit_rooturl();
73         size_t len = strlen(root);
74         if (len && root[len - 1] == '/')
75                 return fmtalloc("%s%s", root, ctx.qry.url);
76         return fmtalloc("%s/%s", root, ctx.qry.url);
77 }
78
79 const char *cgit_rooturl(void)
80 {
81         if (ctx.cfg.virtual_root)
82                 return ctx.cfg.virtual_root;
83         else
84                 return ctx.cfg.script_name;
85 }
86
87 const char *cgit_loginurl(void)
88 {
89         static const char *login_url;
90         if (!login_url)
91                 login_url = fmtalloc("%s?p=login", cgit_rooturl());
92         return login_url;
93 }
94
95 char *cgit_repourl(const char *reponame)
96 {
97         if (ctx.cfg.virtual_root)
98                 return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
99         else
100                 return fmtalloc("?r=%s", reponame);
101 }
102
103 char *cgit_fileurl(const char *reponame, const char *pagename,
104                    const char *filename, const char *query)
105 {
106         struct strbuf sb = STRBUF_INIT;
107         char *delim;
108
109         if (ctx.cfg.virtual_root) {
110                 strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
111                             pagename, (filename ? filename:""));
112                 delim = "?";
113         } else {
114                 strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
115                             (filename ? filename : ""));
116                 delim = "&amp;";
117         }
118         if (query)
119                 strbuf_addf(&sb, "%s%s", delim, query);
120         return strbuf_detach(&sb, NULL);
121 }
122
123 char *cgit_pageurl(const char *reponame, const char *pagename,
124                    const char *query)
125 {
126         return cgit_fileurl(reponame, pagename, NULL, query);
127 }
128
129 const char *cgit_repobasename(const char *reponame)
130 {
131         /* I assume we don't need to store more than one repo basename */
132         static char rvbuf[1024];
133         int p;
134         const char *rv;
135         strncpy(rvbuf, reponame, sizeof(rvbuf));
136         if (rvbuf[sizeof(rvbuf)-1])
137                 die("cgit_repobasename: truncated repository name '%s'", reponame);
138         p = strlen(rvbuf)-1;
139         /* strip trailing slashes */
140         while (p && rvbuf[p] == '/') rvbuf[p--] = 0;
141         /* strip trailing .git */
142         if (p >= 3 && starts_with(&rvbuf[p-3], ".git")) {
143                 p -= 3; rvbuf[p--] = 0;
144         }
145         /* strip more trailing slashes if any */
146         while ( p && rvbuf[p] == '/') rvbuf[p--] = 0;
147         /* find last slash in the remaining string */
148         rv = strrchr(rvbuf,'/');
149         if (rv)
150                 return ++rv;
151         return rvbuf;
152 }
153
154 static void site_url(const char *page, const char *search, const char *sort, int ofs, int always_root)
155 {
156         char *delim = "?";
157
158         if (always_root || page)
159                 html_attr(cgit_rooturl());
160         else
161                 html_attr(cgit_currenturl());
162
163         if (page) {
164                 htmlf("?p=%s", page);
165                 delim = "&amp;";
166         }
167         if (search) {
168                 html(delim);
169                 html("q=");
170                 html_attr(search);
171                 delim = "&amp;";
172         }
173         if (sort) {
174                 html(delim);
175                 html("s=");
176                 html_attr(sort);
177                 delim = "&amp;";
178         }
179         if (ofs) {
180                 html(delim);
181                 htmlf("ofs=%d", ofs);
182         }
183 }
184
185 static void site_link(const char *page, const char *name, const char *title,
186                       const char *class, const char *search, const char *sort, int ofs, int always_root)
187 {
188         html("<a");
189         if (title) {
190                 html(" title='");
191                 html_attr(title);
192                 html("'");
193         }
194         if (class) {
195                 html(" class='");
196                 html_attr(class);
197                 html("'");
198         }
199         html(" href='");
200         site_url(page, search, sort, ofs, always_root);
201         html("'>");
202         html_txt(name);
203         html("</a>");
204 }
205
206 void cgit_index_link(const char *name, const char *title, const char *class,
207                      const char *pattern, const char *sort, int ofs, int always_root)
208 {
209         site_link(NULL, name, title, class, pattern, sort, ofs, always_root);
210 }
211
212 static char *repolink(const char *title, const char *class, const char *page,
213                       const char *head, const char *path)
214 {
215         char *delim = "?";
216
217         html("<a");
218         if (title) {
219                 html(" title='");
220                 html_attr(title);
221                 html("'");
222         }
223         if (class) {
224                 html(" class='");
225                 html_attr(class);
226                 html("'");
227         }
228         html(" href='");
229         if (ctx.cfg.virtual_root) {
230                 html_url_path(ctx.cfg.virtual_root);
231                 html_url_path(ctx.repo->url);
232                 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
233                         html("/");
234                 if (page) {
235                         html_url_path(page);
236                         html("/");
237                         if (path)
238                                 html_url_path(path);
239                 }
240         } else {
241                 html_url_path(ctx.cfg.script_name);
242                 html("?url=");
243                 html_url_arg(ctx.repo->url);
244                 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
245                         html("/");
246                 if (page) {
247                         html_url_arg(page);
248                         html("/");
249                         if (path)
250                                 html_url_arg(path);
251                 }
252                 delim = "&amp;";
253         }
254         if (head && strcmp(head, ctx.repo->defbranch)) {
255                 html(delim);
256                 html("h=");
257                 html_url_arg(head);
258                 delim = "&amp;";
259         }
260         return fmt("%s", delim);
261 }
262
263 static void reporevlink(const char *page, const char *name, const char *title,
264                         const char *class, const char *head, const char *rev,
265                         const char *path)
266 {
267         char *delim;
268
269         delim = repolink(title, class, page, head, path);
270         if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
271                 html(delim);
272                 html("id=");
273                 html_url_arg(rev);
274         }
275         html("'>");
276         html_txt(name);
277         html("</a>");
278 }
279
280 void cgit_summary_link(const char *name, const char *title, const char *class,
281                        const char *head)
282 {
283         reporevlink(NULL, name, title, class, head, NULL, NULL);
284 }
285
286 void cgit_tag_link(const char *name, const char *title, const char *class,
287                    const char *tag)
288 {
289         reporevlink("tag", name, title, class, tag, NULL, NULL);
290 }
291
292 void cgit_tree_link(const char *name, const char *title, const char *class,
293                     const char *head, const char *rev, const char *path)
294 {
295         reporevlink("tree", name, title, class, head, rev, path);
296 }
297
298 void cgit_plain_link(const char *name, const char *title, const char *class,
299                      const char *head, const char *rev, const char *path)
300 {
301         reporevlink("plain", name, title, class, head, rev, path);
302 }
303
304 void cgit_log_link(const char *name, const char *title, const char *class,
305                    const char *head, const char *rev, const char *path,
306                    int ofs, const char *grep, const char *pattern, int showmsg)
307 {
308         char *delim;
309
310         delim = repolink(title, class, "log", head, path);
311         if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
312                 html(delim);
313                 html("id=");
314                 html_url_arg(rev);
315                 delim = "&amp;";
316         }
317         if (grep && pattern) {
318                 html(delim);
319                 html("qt=");
320                 html_url_arg(grep);
321                 delim = "&amp;";
322                 html(delim);
323                 html("q=");
324                 html_url_arg(pattern);
325         }
326         if (ofs > 0) {
327                 html(delim);
328                 html("ofs=");
329                 htmlf("%d", ofs);
330                 delim = "&amp;";
331         }
332         if (showmsg) {
333                 html(delim);
334                 html("showmsg=1");
335         }
336         html("'>");
337         html_txt(name);
338         html("</a>");
339 }
340
341 void cgit_commit_link(char *name, const char *title, const char *class,
342                       const char *head, const char *rev, const char *path)
343 {
344         if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
345                 name[ctx.cfg.max_msg_len] = '\0';
346                 name[ctx.cfg.max_msg_len - 1] = '.';
347                 name[ctx.cfg.max_msg_len - 2] = '.';
348                 name[ctx.cfg.max_msg_len - 3] = '.';
349         }
350
351         char *delim;
352
353         delim = repolink(title, class, "commit", head, path);
354         if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
355                 html(delim);
356                 html("id=");
357                 html_url_arg(rev);
358                 delim = "&amp;";
359         }
360         if (ctx.qry.difftype) {
361                 html(delim);
362                 htmlf("dt=%d", ctx.qry.difftype);
363                 delim = "&amp;";
364         }
365         if (ctx.qry.context > 0 && ctx.qry.context != 3) {
366                 html(delim);
367                 html("context=");
368                 htmlf("%d", ctx.qry.context);
369                 delim = "&amp;";
370         }
371         if (ctx.qry.ignorews) {
372                 html(delim);
373                 html("ignorews=1");
374                 delim = "&amp;";
375         }
376         html("'>");
377         if (name[0] != '\0')
378                 html_txt(name);
379         else
380                 html_txt("(no commit message)");
381         html("</a>");
382 }
383
384 void cgit_refs_link(const char *name, const char *title, const char *class,
385                     const char *head, const char *rev, const char *path)
386 {
387         reporevlink("refs", name, title, class, head, rev, path);
388 }
389
390 void cgit_snapshot_link(const char *name, const char *title, const char *class,
391                         const char *head, const char *rev,
392                         const char *archivename)
393 {
394         reporevlink("snapshot", name, title, class, head, rev, archivename);
395 }
396
397 void cgit_diff_link(const char *name, const char *title, const char *class,
398                     const char *head, const char *new_rev, const char *old_rev,
399                     const char *path)
400 {
401         char *delim;
402
403         delim = repolink(title, class, "diff", head, path);
404         if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
405                 html(delim);
406                 html("id=");
407                 html_url_arg(new_rev);
408                 delim = "&amp;";
409         }
410         if (old_rev) {
411                 html(delim);
412                 html("id2=");
413                 html_url_arg(old_rev);
414                 delim = "&amp;";
415         }
416         if (ctx.qry.difftype) {
417                 html(delim);
418                 htmlf("dt=%d", ctx.qry.difftype);
419                 delim = "&amp;";
420         }
421         if (ctx.qry.context > 0 && ctx.qry.context != 3) {
422                 html(delim);
423                 html("context=");
424                 htmlf("%d", ctx.qry.context);
425                 delim = "&amp;";
426         }
427         if (ctx.qry.ignorews) {
428                 html(delim);
429                 html("ignorews=1");
430                 delim = "&amp;";
431         }
432         html("'>");
433         html_txt(name);
434         html("</a>");
435 }
436
437 void cgit_patch_link(const char *name, const char *title, const char *class,
438                      const char *head, const char *rev, const char *path)
439 {
440         reporevlink("patch", name, title, class, head, rev, path);
441 }
442
443 void cgit_stats_link(const char *name, const char *title, const char *class,
444                      const char *head, const char *path)
445 {
446         reporevlink("stats", name, title, class, head, NULL, path);
447 }
448
449 static void cgit_self_link(char *name, const char *title, const char *class)
450 {
451         if (!strcmp(ctx.qry.page, "repolist"))
452                 cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
453                                 ctx.qry.ofs, 1);
454         else if (!strcmp(ctx.qry.page, "summary"))
455                 cgit_summary_link(name, title, class, ctx.qry.head);
456         else if (!strcmp(ctx.qry.page, "tag"))
457                 cgit_tag_link(name, title, class, ctx.qry.has_sha1 ?
458                                ctx.qry.sha1 : ctx.qry.head);
459         else if (!strcmp(ctx.qry.page, "tree"))
460                 cgit_tree_link(name, title, class, ctx.qry.head,
461                                ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
462                                ctx.qry.path);
463         else if (!strcmp(ctx.qry.page, "plain"))
464                 cgit_plain_link(name, title, class, ctx.qry.head,
465                                 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
466                                 ctx.qry.path);
467         else if (!strcmp(ctx.qry.page, "log"))
468                 cgit_log_link(name, title, class, ctx.qry.head,
469                               ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
470                               ctx.qry.path, ctx.qry.ofs,
471                               ctx.qry.grep, ctx.qry.search,
472                               ctx.qry.showmsg);
473         else if (!strcmp(ctx.qry.page, "commit"))
474                 cgit_commit_link(name, title, class, ctx.qry.head,
475                                  ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
476                                  ctx.qry.path);
477         else if (!strcmp(ctx.qry.page, "patch"))
478                 cgit_patch_link(name, title, class, ctx.qry.head,
479                                 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
480                                 ctx.qry.path);
481         else if (!strcmp(ctx.qry.page, "refs"))
482                 cgit_refs_link(name, title, class, ctx.qry.head,
483                                ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
484                                ctx.qry.path);
485         else if (!strcmp(ctx.qry.page, "snapshot"))
486                 cgit_snapshot_link(name, title, class, ctx.qry.head,
487                                    ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
488                                    ctx.qry.path);
489         else if (!strcmp(ctx.qry.page, "diff"))
490                 cgit_diff_link(name, title, class, ctx.qry.head,
491                                ctx.qry.sha1, ctx.qry.sha2,
492                                ctx.qry.path);
493         else if (!strcmp(ctx.qry.page, "stats"))
494                 cgit_stats_link(name, title, class, ctx.qry.head,
495                                 ctx.qry.path);
496         else {
497                 /* Don't known how to make link for this page */
498                 repolink(title, class, ctx.qry.page, ctx.qry.head, ctx.qry.path);
499                 html("><!-- cgit_self_link() doesn't know how to make link for page '");
500                 html_txt(ctx.qry.page);
501                 html("' -->");
502                 html_txt(name);
503                 html("</a>");
504         }
505 }
506
507 void cgit_object_link(struct object *obj)
508 {
509         char *page, *shortrev, *fullrev, *name;
510
511         fullrev = sha1_to_hex(obj->sha1);
512         shortrev = xstrdup(fullrev);
513         shortrev[10] = '\0';
514         if (obj->type == OBJ_COMMIT) {
515                 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
516                                  ctx.qry.head, fullrev, NULL);
517                 return;
518         } else if (obj->type == OBJ_TREE)
519                 page = "tree";
520         else if (obj->type == OBJ_TAG)
521                 page = "tag";
522         else
523                 page = "blob";
524         name = fmt("%s %s...", typename(obj->type), shortrev);
525         reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
526 }
527
528 static struct string_list_item *lookup_path(struct string_list *list,
529                                             const char *path)
530 {
531         struct string_list_item *item;
532
533         while (path && path[0]) {
534                 if ((item = string_list_lookup(list, path)))
535                         return item;
536                 if (!(path = strchr(path, '/')))
537                         break;
538                 path++;
539         }
540         return NULL;
541 }
542
543 void cgit_submodule_link(const char *class, char *path, const char *rev)
544 {
545         struct string_list *list;
546         struct string_list_item *item;
547         char tail, *dir;
548         size_t len;
549
550         len = 0;
551         tail = 0;
552         list = &ctx.repo->submodules;
553         item = lookup_path(list, path);
554         if (!item) {
555                 len = strlen(path);
556                 tail = path[len - 1];
557                 if (tail == '/') {
558                         path[len - 1] = 0;
559                         item = lookup_path(list, path);
560                 }
561         }
562         if (item || ctx.repo->module_link) {
563                 html("<a ");
564                 if (class)
565                         htmlf("class='%s' ", class);
566                 html("href='");
567                 if (item) {
568                         html_attrf(item->util, rev);
569                 } else {
570                         dir = strrchr(path, '/');
571                         if (dir)
572                                 dir++;
573                         else
574                                 dir = path;
575                         html_attrf(ctx.repo->module_link, dir, rev);
576                 }
577                 html("'>");
578                 html_txt(path);
579                 html("</a>");
580         } else {
581                 html("<span");
582                 if (class)
583                         htmlf(" class='%s'", class);
584                 html(">");
585                 html_txt(path);
586                 html("</span>");
587         }
588         html_txtf(" @ %.7s", rev);
589         if (item && tail)
590                 path[len - 1] = tail;
591 }
592
593 void cgit_print_date(time_t secs, const char *format, int local_time)
594 {
595         char buf[64];
596         struct tm *time;
597
598         if (!secs)
599                 return;
600         if (local_time)
601                 time = localtime(&secs);
602         else
603                 time = gmtime(&secs);
604         strftime(buf, sizeof(buf)-1, format, time);
605         html_txt(buf);
606 }
607
608 static void print_rel_date(time_t t, double value,
609         const char *class, const char *suffix)
610 {
611         char buf[64];
612         struct tm *time;
613
614         if (ctx.cfg.local_time)
615                 time = localtime(&t);
616         else
617                 time = gmtime(&t);
618         strftime(buf, sizeof(buf) - 1, FMT_LONGDATE, time);
619
620         htmlf("<span class='%s' title='", class);
621         html_attr(buf);
622         htmlf("'>%.0f %s</span>", value, suffix);
623 }
624
625 void cgit_print_age(time_t t, time_t max_relative, const char *format)
626 {
627         time_t now, secs;
628
629         if (!t)
630                 return;
631         time(&now);
632         secs = now - t;
633         if (secs < 0)
634                 secs = 0;
635
636         if (secs > max_relative && max_relative >= 0) {
637                 cgit_print_date(t, format, ctx.cfg.local_time);
638                 return;
639         }
640
641         if (secs < TM_HOUR * 2) {
642                 print_rel_date(t, secs * 1.0 / TM_MIN, "age-mins", "min.");
643                 return;
644         }
645         if (secs < TM_DAY * 2) {
646                 print_rel_date(t, secs * 1.0 / TM_HOUR, "age-hours", "hours");
647                 return;
648         }
649         if (secs < TM_WEEK * 2) {
650                 print_rel_date(t, secs * 1.0 / TM_DAY, "age-days", "days");
651                 return;
652         }
653         if (secs < TM_MONTH * 2) {
654                 print_rel_date(t, secs * 1.0 / TM_WEEK, "age-weeks", "weeks");
655                 return;
656         }
657         if (secs < TM_YEAR * 2) {
658                 print_rel_date(t, secs * 1.0 / TM_MONTH, "age-months", "months");
659                 return;
660         }
661         print_rel_date(t, secs * 1.0 / TM_YEAR, "age-years", "years");
662 }
663
664 void cgit_print_http_headers(void)
665 {
666         if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
667                 return;
668
669         if (ctx.page.status)
670                 htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
671         if (ctx.page.mimetype && ctx.page.charset)
672                 htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
673                       ctx.page.charset);
674         else if (ctx.page.mimetype)
675                 htmlf("Content-Type: %s\n", ctx.page.mimetype);
676         if (ctx.page.size)
677                 htmlf("Content-Length: %zd\n", ctx.page.size);
678         if (ctx.page.filename)
679                 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
680                       ctx.page.filename);
681         if (!ctx.env.authenticated)
682                 html("Cache-Control: no-cache, no-store\n");
683         htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
684         htmlf("Expires: %s\n", http_date(ctx.page.expires));
685         if (ctx.page.etag)
686                 htmlf("ETag: \"%s\"\n", ctx.page.etag);
687         html("\n");
688         if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
689                 exit(0);
690 }
691
692 void cgit_redirect(const char *url, bool permanent)
693 {
694         htmlf("Status: %d %s\n", permanent ? 301 : 302, permanent ? "Moved" : "Found");
695         htmlf("Location: %s\n", url);
696         htmlf("\n");
697         exit(0);
698 }
699
700 static void print_rel_vcs_link(const char *url)
701 {
702         html("<link rel='vcs-git' href='");
703         html_attr(url);
704         html("' title='");
705         html_attr(ctx.repo->name);
706         html(" Git repository'/>\n");
707 }
708
709 void cgit_print_docstart(void)
710 {
711         if (ctx.cfg.embedded) {
712                 if (ctx.cfg.header)
713                         html_include(ctx.cfg.header);
714                 return;
715         }
716
717         const char *host = cgit_hosturl();
718         html(cgit_doctype);
719         html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
720         html("<head>\n");
721         html("<title>");
722         html_txt(ctx.page.title);
723         html("</title>\n");
724         htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
725         if (ctx.cfg.robots && *ctx.cfg.robots)
726                 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
727         html("<link rel='stylesheet' type='text/css' href='");
728         html_attr(ctx.cfg.css);
729         html("'/>\n");
730         if (ctx.cfg.favicon) {
731                 html("<link rel='shortcut icon' href='");
732                 html_attr(ctx.cfg.favicon);
733                 html("'/>\n");
734         }
735         if (host && ctx.repo && ctx.qry.head) {
736                 struct strbuf sb = STRBUF_INIT;
737                 strbuf_addf(&sb, "h=%s", ctx.qry.head);
738
739                 html("<link rel='alternate' title='Atom feed' href='");
740                 html(cgit_httpscheme());
741                 html_attr(cgit_hosturl());
742                 html_attr(cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
743                                        sb.buf));
744                 html("' type='application/atom+xml'/>\n");
745                 strbuf_release(&sb);
746         }
747         if (ctx.repo)
748                 cgit_add_clone_urls(print_rel_vcs_link);
749         if (ctx.cfg.head_include)
750                 html_include(ctx.cfg.head_include);
751         html("</head>\n");
752         html("<body>\n");
753         if (ctx.cfg.header)
754                 html_include(ctx.cfg.header);
755 }
756
757 void cgit_print_docend(void)
758 {
759         html("</div> <!-- class=content -->\n");
760         if (ctx.cfg.embedded) {
761                 html("</div> <!-- id=cgit -->\n");
762                 if (ctx.cfg.footer)
763                         html_include(ctx.cfg.footer);
764                 return;
765         }
766         if (ctx.cfg.footer)
767                 html_include(ctx.cfg.footer);
768         else {
769                 htmlf("<div class='footer'>generated by <a href='http://git.zx2c4.com/cgit/about/'>cgit %s</a> at ",
770                         cgit_version);
771                 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
772                 html("</div>\n");
773         }
774         html("</div> <!-- id=cgit -->\n");
775         html("</body>\n</html>\n");
776 }
777
778 static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
779 {
780         struct strbuf **url_list = strbuf_split_str(txt, ' ', 0);
781         int i;
782
783         for (i = 0; url_list[i]; i++) {
784                 strbuf_rtrim(url_list[i]);
785                 if (url_list[i]->len == 0)
786                         continue;
787                 if (suffix && *suffix)
788                         strbuf_addf(url_list[i], "/%s", suffix);
789                 fn(url_list[i]->buf);
790         }
791
792         strbuf_list_free(url_list);
793 }
794
795 void cgit_add_clone_urls(void (*fn)(const char *))
796 {
797         if (ctx.repo->clone_url)
798                 add_clone_urls(fn, expand_macros(ctx.repo->clone_url), NULL);
799         else if (ctx.cfg.clone_prefix)
800                 add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
801 }
802
803 static int print_branch_option(const char *refname, const struct object_id *oid,
804                                int flags, void *cb_data)
805 {
806         char *name = (char *)refname;
807         html_option(name, name, ctx.qry.head);
808         return 0;
809 }
810
811 void cgit_add_hidden_formfields(int incl_head, int incl_search,
812                                 const char *page)
813 {
814         if (!ctx.cfg.virtual_root) {
815                 struct strbuf url = STRBUF_INIT;
816
817                 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
818                 if (ctx.qry.vpath)
819                         strbuf_addf(&url, "/%s", ctx.qry.vpath);
820                 html_hidden("url", url.buf);
821                 strbuf_release(&url);
822         }
823
824         if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
825             strcmp(ctx.qry.head, ctx.repo->defbranch))
826                 html_hidden("h", ctx.qry.head);
827
828         if (ctx.qry.sha1)
829                 html_hidden("id", ctx.qry.sha1);
830         if (ctx.qry.sha2)
831                 html_hidden("id2", ctx.qry.sha2);
832         if (ctx.qry.showmsg)
833                 html_hidden("showmsg", "1");
834
835         if (incl_search) {
836                 if (ctx.qry.grep)
837                         html_hidden("qt", ctx.qry.grep);
838                 if (ctx.qry.search)
839                         html_hidden("q", ctx.qry.search);
840         }
841 }
842
843 static const char *hc(const char *page)
844 {
845         return strcmp(ctx.qry.page, page) ? NULL : "active";
846 }
847
848 static void cgit_print_path_crumbs(char *path)
849 {
850         char *old_path = ctx.qry.path;
851         char *p = path, *q, *end = path + strlen(path);
852
853         ctx.qry.path = NULL;
854         cgit_self_link("root", NULL, NULL);
855         ctx.qry.path = p = path;
856         while (p < end) {
857                 if (!(q = strchr(p, '/')))
858                         q = end;
859                 *q = '\0';
860                 html_txt("/");
861                 cgit_self_link(p, NULL, NULL);
862                 if (q < end)
863                         *q = '/';
864                 p = q + 1;
865         }
866         ctx.qry.path = old_path;
867 }
868
869 static void print_header(void)
870 {
871         char *logo = NULL, *logo_link = NULL;
872
873         html("<table id='header'>\n");
874         html("<tr>\n");
875
876         if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
877                 logo = ctx.repo->logo;
878         else
879                 logo = ctx.cfg.logo;
880         if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
881                 logo_link = ctx.repo->logo_link;
882         else
883                 logo_link = ctx.cfg.logo_link;
884         if (logo && *logo) {
885                 html("<td class='logo' rowspan='2'><a href='");
886                 if (logo_link && *logo_link)
887                         html_attr(logo_link);
888                 else
889                         html_attr(cgit_rooturl());
890                 html("'><img src='");
891                 html_attr(logo);
892                 html("' alt='cgit logo'/></a></td>\n");
893         }
894
895         html("<td class='main'>");
896         if (ctx.repo) {
897                 cgit_index_link("index", NULL, NULL, NULL, NULL, 0, 1);
898                 html(" : ");
899                 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
900                 if (ctx.env.authenticated) {
901                         html("</td><td class='form'>");
902                         html("<form method='get' action=''>\n");
903                         cgit_add_hidden_formfields(0, 1, ctx.qry.page);
904                         html("<select name='h' onchange='this.form.submit();'>\n");
905                         for_each_branch_ref(print_branch_option, ctx.qry.head);
906                         if (ctx.repo->enable_remote_branches)
907                                 for_each_remote_ref(print_branch_option, ctx.qry.head);
908                         html("</select> ");
909                         html("<input type='submit' name='' value='switch'/>");
910                         html("</form>");
911                 }
912         } else
913                 html_txt(ctx.cfg.root_title);
914         html("</td></tr>\n");
915
916         html("<tr><td class='sub'>");
917         if (ctx.repo) {
918                 html_txt(ctx.repo->desc);
919                 html("</td><td class='sub right'>");
920                 html_txt(ctx.repo->owner);
921         } else {
922                 if (ctx.cfg.root_desc)
923                         html_txt(ctx.cfg.root_desc);
924                 else if (ctx.cfg.index_info)
925                         html_include(ctx.cfg.index_info);
926         }
927         html("</td></tr></table>\n");
928 }
929
930 void cgit_print_pageheader(void)
931 {
932         html("<div id='cgit'>");
933         if (!ctx.env.authenticated || !ctx.cfg.noheader)
934                 print_header();
935
936         html("<table class='tabs'><tr><td>\n");
937         if (ctx.env.authenticated && ctx.repo) {
938                 if (ctx.repo->readme.nr)
939                         reporevlink("about", "about", NULL,
940                                     hc("about"), ctx.qry.head, NULL,
941                                     NULL);
942                 cgit_summary_link("summary", NULL, hc("summary"),
943                                   ctx.qry.head);
944                 cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
945                                ctx.qry.sha1, NULL);
946                 cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
947                               NULL, ctx.qry.vpath, 0, NULL, NULL,
948                               ctx.qry.showmsg);
949                 cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
950                                ctx.qry.sha1, ctx.qry.vpath);
951                 cgit_commit_link("commit", NULL, hc("commit"),
952                                  ctx.qry.head, ctx.qry.sha1, ctx.qry.vpath);
953                 cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
954                                ctx.qry.sha1, ctx.qry.sha2, ctx.qry.vpath);
955                 if (ctx.repo->max_stats)
956                         cgit_stats_link("stats", NULL, hc("stats"),
957                                         ctx.qry.head, ctx.qry.vpath);
958                 html("</td><td class='form'>");
959                 html("<form class='right' method='get' action='");
960                 if (ctx.cfg.virtual_root)
961                         html_url_path(cgit_fileurl(ctx.qry.repo, "log",
962                                                    ctx.qry.vpath, NULL));
963                 html("'>\n");
964                 cgit_add_hidden_formfields(1, 0, "log");
965                 html("<select name='qt'>\n");
966                 html_option("grep", "log msg", ctx.qry.grep);
967                 html_option("author", "author", ctx.qry.grep);
968                 html_option("committer", "committer", ctx.qry.grep);
969                 html_option("range", "range", ctx.qry.grep);
970                 html("</select>\n");
971                 html("<input class='txt' type='text' size='10' name='q' value='");
972                 html_attr(ctx.qry.search);
973                 html("'/>\n");
974                 html("<input type='submit' value='search'/>\n");
975                 html("</form>\n");
976         } else if (ctx.env.authenticated) {
977                 site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
978                 if (ctx.cfg.root_readme)
979                         site_link("about", "about", NULL, hc("about"),
980                                   NULL, NULL, 0, 1);
981                 html("</td><td class='form'>");
982                 html("<form method='get' action='");
983                 html_attr(cgit_currenturl());
984                 html("'>\n");
985                 html("<input type='text' name='q' size='10' value='");
986                 html_attr(ctx.qry.search);
987                 html("'/>\n");
988                 html("<input type='submit' value='search'/>\n");
989                 html("</form>");
990         }
991         html("</td></tr></table>\n");
992         if (ctx.env.authenticated && ctx.qry.vpath) {
993                 html("<div class='path'>");
994                 html("path: ");
995                 cgit_print_path_crumbs(ctx.qry.vpath);
996                 html("</div>");
997         }
998         html("<div class='content'>");
999 }
1000
1001 void cgit_print_filemode(unsigned short mode)
1002 {
1003         if (S_ISDIR(mode))
1004                 html("d");
1005         else if (S_ISLNK(mode))
1006                 html("l");
1007         else if (S_ISGITLINK(mode))
1008                 html("m");
1009         else
1010                 html("-");
1011         html_fileperm(mode >> 6);
1012         html_fileperm(mode >> 3);
1013         html_fileperm(mode);
1014 }
1015
1016 void cgit_print_snapshot_links(const char *repo, const char *head,
1017                                const char *hex, int snapshots)
1018 {
1019         const struct cgit_snapshot_format* f;
1020         struct strbuf filename = STRBUF_INIT;
1021         size_t prefixlen;
1022         unsigned char sha1[20];
1023
1024         if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
1025             (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
1026                 hex++;
1027         strbuf_addf(&filename, "%s-%s", cgit_repobasename(repo), hex);
1028         prefixlen = filename.len;
1029         for (f = cgit_snapshot_formats; f->suffix; f++) {
1030                 if (!(snapshots & f->bit))
1031                         continue;
1032                 strbuf_setlen(&filename, prefixlen);
1033                 strbuf_addstr(&filename, f->suffix);
1034                 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
1035                                    filename.buf);
1036                 html("<br/>");
1037         }
1038         strbuf_release(&filename);
1039 }