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