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