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