summaryrefslogtreecommitdiffabout
path: root/ui-shared.c
Unidiff
Diffstat (limited to 'ui-shared.c') (more/less context) (show whitespace changes)
-rw-r--r--ui-shared.c237
1 files changed, 129 insertions, 108 deletions
diff --git a/ui-shared.c b/ui-shared.c
index 60aa2e3..aa65988 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -9,2 +9,3 @@
9#include "cgit.h" 9#include "cgit.h"
10#include "html.h"
10 11
@@ -27,10 +28,2 @@ static char *http_date(time_t t)
27 28
28static long ttl_seconds(long ttl)
29{
30 if (ttl<0)
31 return 60 * 60 * 24 * 365;
32 else
33 return ttl * 60;
34}
35
36void cgit_print_error(char *msg) 29void cgit_print_error(char *msg)
@@ -44,6 +37,6 @@ char *cgit_rooturl()
44{ 37{
45 if (cgit_virtual_root) 38 if (ctx.cfg.virtual_root)
46 return fmt("%s/", cgit_virtual_root); 39 return fmt("%s/", ctx.cfg.virtual_root);
47 else 40 else
48 return cgit_script_name; 41 return ctx.cfg.script_name;
49} 42}
@@ -52,4 +45,4 @@ char *cgit_repourl(const char *reponame)
52{ 45{
53 if (cgit_virtual_root) { 46 if (ctx.cfg.virtual_root) {
54 return fmt("%s/%s/", cgit_virtual_root, reponame); 47 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
55 } else { 48 } else {
@@ -65,4 +58,4 @@ char *cgit_fileurl(const char *reponame, const char *pagename,
65 58
66 if (cgit_virtual_root) { 59 if (ctx.cfg.virtual_root) {
67 tmp = fmt("%s/%s/%s/%s", cgit_virtual_root, reponame, 60 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
68 pagename, (filename ? filename:"")); 61 pagename, (filename ? filename:""));
@@ -112,10 +105,10 @@ char *cgit_currurl()
112{ 105{
113 if (!cgit_virtual_root) 106 if (!ctx.cfg.virtual_root)
114 return cgit_script_name; 107 return ctx.cfg.script_name;
115 else if (cgit_query_page) 108 else if (ctx.qry.page)
116 return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); 109 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
117 else if (cgit_query_repo) 110 else if (ctx.qry.repo)
118 return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); 111 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
119 else 112 else
120 return fmt("%s/", cgit_virtual_root); 113 return fmt("%s/", ctx.cfg.virtual_root);
121} 114}
@@ -139,8 +132,8 @@ static char *repolink(char *title, char *class, char *page, char *head,
139 html(" href='"); 132 html(" href='");
140 if (cgit_virtual_root) { 133 if (ctx.cfg.virtual_root) {
141 html_attr(cgit_virtual_root); 134 html_attr(ctx.cfg.virtual_root);
142 if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/') 135 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
143 html("/"); 136 html("/");
144 html_attr(cgit_repo->url); 137 html_attr(ctx.repo->url);
145 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') 138 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
146 html("/"); 139 html("/");
@@ -153,6 +146,6 @@ static char *repolink(char *title, char *class, char *page, char *head,
153 } else { 146 } else {
154 html(cgit_script_name); 147 html(ctx.cfg.script_name);
155 html("?url="); 148 html("?url=");
156 html_attr(cgit_repo->url); 149 html_attr(ctx.repo->url);
157 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') 150 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
158 html("/"); 151 html("/");
@@ -166,3 +159,3 @@ static char *repolink(char *title, char *class, char *page, char *head,
166 } 159 }
167 if (head && strcmp(head, cgit_repo->defbranch)) { 160 if (head && strcmp(head, ctx.repo->defbranch)) {
168 html(delim); 161 html(delim);
@@ -181,3 +174,3 @@ static void reporevlink(char *page, char *name, char *title, char *class,
181 delim = repolink(title, class, page, head, path); 174 delim = repolink(title, class, page, head, path);
182 if (rev && strcmp(rev, cgit_query_head)) { 175 if (rev && strcmp(rev, ctx.qry.head)) {
183 html(delim); 176 html(delim);
@@ -203,3 +196,3 @@ void cgit_log_link(char *name, char *title, char *class, char *head,
203 delim = repolink(title, class, "log", head, path); 196 delim = repolink(title, class, "log", head, path);
204 if (rev && strcmp(rev, cgit_query_head)) { 197 if (rev && strcmp(rev, ctx.qry.head)) {
205 html(delim); 198 html(delim);
@@ -231,7 +224,7 @@ void cgit_commit_link(char *name, char *title, char *class, char *head,
231{ 224{
232 if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) { 225 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
233 name[cgit_max_msg_len] = '\0'; 226 name[ctx.cfg.max_msg_len] = '\0';
234 name[cgit_max_msg_len - 1] = '.'; 227 name[ctx.cfg.max_msg_len - 1] = '.';
235 name[cgit_max_msg_len - 2] = '.'; 228 name[ctx.cfg.max_msg_len - 2] = '.';
236 name[cgit_max_msg_len - 3] = '.'; 229 name[ctx.cfg.max_msg_len - 3] = '.';
237 } 230 }
@@ -258,3 +251,3 @@ void cgit_diff_link(char *name, char *title, char *class, char *head,
258 delim = repolink(title, class, "diff", head, path); 251 delim = repolink(title, class, "diff", head, path);
259 if (new_rev && strcmp(new_rev, cgit_query_head)) { 252 if (new_rev && strcmp(new_rev, ctx.qry.head)) {
260 html(delim); 253 html(delim);
@@ -286,3 +279,3 @@ void cgit_object_link(struct object *obj)
286 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, 279 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
287 cgit_query_head, sha1_to_hex(obj->sha1)); 280 ctx.qry.head, sha1_to_hex(obj->sha1));
288 return; 281 return;
@@ -299,3 +292,3 @@ void cgit_object_link(struct object *obj)
299 292
300 url = cgit_pageurl(cgit_query_repo, page, 293 url = cgit_pageurl(ctx.qry.repo, page,
301 fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); 294 fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
@@ -362,9 +355,19 @@ void cgit_print_age(time_t t, time_t max_relative, char *format)
362 355
363void cgit_print_docstart(char *title, struct cacheitem *item) 356void cgit_print_http_headers(struct cgit_context *ctx)
364{ 357{
365 html("Content-Type: text/html; charset=" PAGE_ENCODING "\n"); 358 if (ctx->page.mimetype && ctx->page.charset)
366 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); 359 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
367 htmlf("Expires: %s\n", http_date(item->st.st_mtime + 360 ctx->page.charset);
368 ttl_seconds(item->ttl))); 361 else if (ctx->page.mimetype)
362 htmlf("Content-Type: %s\n", ctx->page.mimetype);
363 if (ctx->page.filename)
364 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
365 ctx->page.filename);
366 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
367 htmlf("Expires: %s\n", http_date(ctx->page.expires));
369 html("\n"); 368 html("\n");
369}
370
371void cgit_print_docstart(struct cgit_context *ctx)
372{
370 html(cgit_doctype); 373 html(cgit_doctype);
@@ -373,9 +376,9 @@ void cgit_print_docstart(char *title, struct cacheitem *item)
373 html("<title>"); 376 html("<title>");
374 html_txt(title); 377 html_txt(ctx->page.title);
375 html("</title>\n"); 378 html("</title>\n");
376 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 379 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
377 if (cgit_robots && *cgit_robots) 380 if (ctx->cfg.robots && *ctx->cfg.robots)
378 htmlf("<meta name='robots' content='%s'/>\n", cgit_robots); 381 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
379 html("<link rel='stylesheet' type='text/css' href='"); 382 html("<link rel='stylesheet' type='text/css' href='");
380 html_attr(cgit_css); 383 html_attr(ctx->cfg.css);
381 html("'/>\n"); 384 html("'/>\n");
@@ -394,3 +397,3 @@ int print_branch_option(const char *refname, const unsigned char *sha1,
394 char *name = (char *)refname; 397 char *name = (char *)refname;
395 html_option(name, name, cgit_query_head); 398 html_option(name, name, ctx.qry.head);
396 return 0; 399 return 0;
@@ -428,3 +431,3 @@ int print_archive_ref(const char *refname, const unsigned char *sha1,
428 } 431 }
429 url = cgit_pageurl(cgit_query_repo, "blob", 432 url = cgit_pageurl(ctx.qry.repo, "blob",
430 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid), 433 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
@@ -441,6 +444,6 @@ void add_hidden_formfields(int incl_head, int incl_search, char *page)
441 444
442 if (!cgit_virtual_root) { 445 if (!ctx.cfg.virtual_root) {
443 url = fmt("%s/%s", cgit_query_repo, page); 446 url = fmt("%s/%s", ctx.qry.repo, page);
444 if (cgit_query_path) 447 if (ctx.qry.path)
445 url = fmt("%s/%s", url, cgit_query_path); 448 url = fmt("%s/%s", url, ctx.qry.path);
446 html_hidden("url", url); 449 html_hidden("url", url);
@@ -448,15 +451,15 @@ void add_hidden_formfields(int incl_head, int incl_search, char *page)
448 451
449 if (incl_head && strcmp(cgit_query_head, cgit_repo->defbranch)) 452 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch))
450 html_hidden("h", cgit_query_head); 453 html_hidden("h", ctx.qry.head);
451 454
452 if (cgit_query_sha1) 455 if (ctx.qry.sha1)
453 html_hidden("id", cgit_query_sha1); 456 html_hidden("id", ctx.qry.sha1);
454 if (cgit_query_sha2) 457 if (ctx.qry.sha2)
455 html_hidden("id2", cgit_query_sha2); 458 html_hidden("id2", ctx.qry.sha2);
456 459
457 if (incl_search) { 460 if (incl_search) {
458 if (cgit_query_grep) 461 if (ctx.qry.grep)
459 html_hidden("qt", cgit_query_grep); 462 html_hidden("qt", ctx.qry.grep);
460 if (cgit_query_search) 463 if (ctx.qry.search)
461 html_hidden("q", cgit_query_search); 464 html_hidden("q", ctx.qry.search);
462 } 465 }
@@ -464,3 +467,3 @@ void add_hidden_formfields(int incl_head, int incl_search, char *page)
464 467
465void cgit_print_pageheader(char *title, int show_search) 468void cgit_print_pageheader(struct cgit_context *ctx)
466{ 469{
@@ -476,26 +479,26 @@ void cgit_print_pageheader(char *title, int show_search)
476 htmlf("'><img src='%s' alt='cgit'/></a>\n", 479 htmlf("'><img src='%s' alt='cgit'/></a>\n",
477 cgit_logo); 480 ctx->cfg.logo);
478 html("</td></tr>\n<tr><td class='sidebar'>\n"); 481 html("</td></tr>\n<tr><td class='sidebar'>\n");
479 if (cgit_query_repo) { 482 if (ctx->repo) {
480 html("<h1 class='first'>"); 483 html("<h1 class='first'>");
481 html_txt(strrpart(cgit_repo->name, 20)); 484 html_txt(strrpart(ctx->repo->name, 20));
482 html("</h1>\n"); 485 html("</h1>\n");
483 html_txt(cgit_repo->desc); 486 html_txt(ctx->repo->desc);
484 if (cgit_repo->owner) { 487 if (ctx->repo->owner) {
485 html("<h1>owner</h1>\n"); 488 html("<h1>owner</h1>\n");
486 html_txt(cgit_repo->owner); 489 html_txt(ctx->repo->owner);
487 } 490 }
488 html("<h1>navigate</h1>\n"); 491 html("<h1>navigate</h1>\n");
489 reporevlink(NULL, "summary", NULL, "menu", cgit_query_head, 492 reporevlink(NULL, "summary", NULL, "menu", ctx->qry.head,
490 NULL, NULL); 493 NULL, NULL);
491 cgit_log_link("log", NULL, "menu", cgit_query_head, NULL, NULL, 494 cgit_log_link("log", NULL, "menu", ctx->qry.head, NULL, NULL,
492 0, NULL, NULL); 495 0, NULL, NULL);
493 cgit_tree_link("tree", NULL, "menu", cgit_query_head, 496 cgit_tree_link("tree", NULL, "menu", ctx->qry.head,
494 cgit_query_sha1, NULL); 497 ctx->qry.sha1, NULL);
495 cgit_commit_link("commit", NULL, "menu", cgit_query_head, 498 cgit_commit_link("commit", NULL, "menu", ctx->qry.head,
496 cgit_query_sha1); 499 ctx->qry.sha1);
497 cgit_diff_link("diff", NULL, "menu", cgit_query_head, 500 cgit_diff_link("diff", NULL, "menu", ctx->qry.head,
498 cgit_query_sha1, cgit_query_sha2, NULL); 501 ctx->qry.sha1, ctx->qry.sha2, NULL);
499 cgit_patch_link("patch", NULL, "menu", cgit_query_head, 502 cgit_patch_link("patch", NULL, "menu", ctx->qry.head,
500 cgit_query_sha1); 503 ctx->qry.sha1);
501 504
@@ -503,9 +506,9 @@ void cgit_print_pageheader(char *title, int show_search)
503 506
504 if (cgit_repo->clone_url || cgit_clone_prefix) { 507 if (ctx->repo->clone_url || ctx->cfg.clone_prefix) {
505 html("<h1>clone</h1>\n"); 508 html("<h1>clone</h1>\n");
506 if (cgit_repo->clone_url) 509 if (ctx->repo->clone_url)
507 url = cgit_repo->clone_url; 510 url = ctx->repo->clone_url;
508 else 511 else
509 url = fmt("%s%s", cgit_clone_prefix, 512 url = fmt("%s%s", ctx->cfg.clone_prefix,
510 cgit_repo->url); 513 ctx->repo->url);
511 html("<a class='menu' href='"); 514 html("<a class='menu' href='");
@@ -521,6 +524,6 @@ void cgit_print_pageheader(char *title, int show_search)
521 html("<form method='get' action=''>\n"); 524 html("<form method='get' action=''>\n");
522 add_hidden_formfields(0, 1, cgit_query_page); 525 add_hidden_formfields(0, 1, ctx->qry.page);
523 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); 526 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>");
524 html("<select name='h' onchange='this.form.submit();'>\n"); 527 html("<select name='h' onchange='this.form.submit();'>\n");
525 for_each_branch_ref(print_branch_option, cgit_query_head); 528 for_each_branch_ref(print_branch_option, ctx->qry.head);
526 html("</select>\n"); 529 html("</select>\n");
@@ -533,5 +536,5 @@ void cgit_print_pageheader(char *title, int show_search)
533 html("<form method='get' action='"); 536 html("<form method='get' action='");
534 if (cgit_virtual_root) 537 if (ctx->cfg.virtual_root)
535 html_attr(cgit_fileurl(cgit_query_repo, "log", 538 html_attr(cgit_fileurl(ctx->qry.repo, "log",
536 cgit_query_path, NULL)); 539 ctx->qry.path, NULL));
537 html("'>\n"); 540 html("'>\n");
@@ -539,8 +542,8 @@ void cgit_print_pageheader(char *title, int show_search)
539 html("<select name='qt'>\n"); 542 html("<select name='qt'>\n");
540 html_option("grep", "log msg", cgit_query_grep); 543 html_option("grep", "log msg", ctx->qry.grep);
541 html_option("author", "author", cgit_query_grep); 544 html_option("author", "author", ctx->qry.grep);
542 html_option("committer", "committer", cgit_query_grep); 545 html_option("committer", "committer", ctx->qry.grep);
543 html("</select>\n"); 546 html("</select>\n");
544 html("<input class='txt' type='text' name='q' value='"); 547 html("<input class='txt' type='text' name='q' value='");
545 html_attr(cgit_query_search); 548 html_attr(ctx->qry.search);
546 html("'/>\n"); 549 html("'/>\n");
@@ -548,3 +551,3 @@ void cgit_print_pageheader(char *title, int show_search)
548 } else { 551 } else {
549 if (!cgit_index_info || html_include(cgit_index_info)) 552 if (!ctx->cfg.index_info || html_include(ctx->cfg.index_info))
550 html(default_info); 553 html(default_info);
@@ -557,14 +560,32 @@ void cgit_print_pageheader(char *title, int show_search)
557 560
558 561void cgit_print_filemode(unsigned short mode)
559void cgit_print_snapshot_start(const char *mimetype, const char *filename,
560 struct cacheitem *item)
561{ 562{
562 htmlf("Content-Type: %s\n", mimetype); 563 if (S_ISDIR(mode))
563 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename); 564 html("d");
564 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); 565 else if (S_ISLNK(mode))
565 htmlf("Expires: %s\n", http_date(item->st.st_mtime + 566 html("l");
566 ttl_seconds(item->ttl))); 567 else if (S_ISGITLINK(mode))
567 html("\n"); 568 html("m");
569 else
570 html("-");
571 html_fileperm(mode >> 6);
572 html_fileperm(mode >> 3);
573 html_fileperm(mode);
568} 574}
569 575
570/* vim:set sw=8: */ 576void cgit_print_snapshot_links(const char *repo, const char *head,
577 const char *hex, int snapshots)
578{
579 const struct cgit_snapshot_format* f;
580 char *filename;
581
582 for (f = cgit_snapshot_formats; f->suffix; f++) {
583 if (!(snapshots & f->bit))
584 continue;
585 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
586 f->suffix);
587 cgit_snapshot_link(filename, NULL, NULL, (char *)head,
588 (char *)hex, filename);
589 html("<br/>");
590 }
591}