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