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