]> gitweb.ps.run Git - ps-cgit/blob - ui-shared.c
Merge branch 'lh/plain'
[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", "Now", "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(char *msg)
31 {
32         html("<div class='error'>");
33         html_txt(msg);
34         html("</div>\n");
35 }
36
37 char *cgit_hosturl()
38 {
39         char *host, *port;
40
41         host = getenv("SERVER_NAME");
42         if (!host)
43                 return NULL;
44         port = getenv("SERVER_PORT");
45         if (port && atoi(port) != 80)
46                 host = xstrdup(fmt("%s:%d", host, atoi(port)));
47         else
48                 host = xstrdup(host);
49         return host;
50 }
51
52 char *cgit_rooturl()
53 {
54         if (ctx.cfg.virtual_root)
55                 return fmt("%s/", ctx.cfg.virtual_root);
56         else
57                 return ctx.cfg.script_name;
58 }
59
60 char *cgit_repourl(const char *reponame)
61 {
62         if (ctx.cfg.virtual_root) {
63                 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
64         } else {
65                 return fmt("?r=%s", reponame);
66         }
67 }
68
69 char *cgit_fileurl(const char *reponame, const char *pagename,
70                    const char *filename, const char *query)
71 {
72         char *tmp;
73         char *delim;
74
75         if (ctx.cfg.virtual_root) {
76                 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
77                           pagename, (filename ? filename:""));
78                 delim = "?";
79         } else {
80                 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
81                           (filename ? filename : ""));
82                 delim = "&";
83         }
84         if (query)
85                 tmp = fmt("%s%s%s", tmp, delim, query);
86         return tmp;
87 }
88
89 char *cgit_pageurl(const char *reponame, const char *pagename,
90                    const char *query)
91 {
92         return cgit_fileurl(reponame,pagename,0,query);
93 }
94
95 const char *cgit_repobasename(const char *reponame)
96 {
97         /* I assume we don't need to store more than one repo basename */
98         static char rvbuf[1024];
99         int p;
100         const char *rv;
101         strncpy(rvbuf,reponame,sizeof(rvbuf));
102         if(rvbuf[sizeof(rvbuf)-1])
103                 die("cgit_repobasename: truncated repository name '%s'", reponame);
104         p = strlen(rvbuf)-1;
105         /* strip trailing slashes */
106         while(p && rvbuf[p]=='/') rvbuf[p--]=0;
107         /* strip trailing .git */
108         if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
109                 p -= 3; rvbuf[p--] = 0;
110         }
111         /* strip more trailing slashes if any */
112         while( p && rvbuf[p]=='/') rvbuf[p--]=0;
113         /* find last slash in the remaining string */
114         rv = strrchr(rvbuf,'/');
115         if(rv)
116                 return ++rv;
117         return rvbuf;
118 }
119
120 char *cgit_currurl()
121 {
122         if (!ctx.cfg.virtual_root)
123                 return ctx.cfg.script_name;
124         else if (ctx.qry.page)
125                 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
126         else if (ctx.qry.repo)
127                 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
128         else
129                 return fmt("%s/", ctx.cfg.virtual_root);
130 }
131
132 static void site_url(char *page, char *search, int ofs)
133 {
134         char *delim = "?";
135
136         if (ctx.cfg.virtual_root) {
137                 html_attr(ctx.cfg.virtual_root);
138                 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
139                         html("/");
140         } else
141                 html(ctx.cfg.script_name);
142
143         if (page) {
144                 htmlf("?p=%s", page);
145                 delim = "&";
146         }
147         if (search) {
148                 html(delim);
149                 html("q=");
150                 html_attr(search);
151                 delim = "&";
152         }
153         if (ofs) {
154                 html(delim);
155                 htmlf("ofs=%d", ofs);
156         }
157 }
158
159 static void site_link(char *page, char *name, char *title, char *class,
160                       char *search, int ofs)
161 {
162         html("<a");
163         if (title) {
164                 html(" title='");
165                 html_attr(title);
166                 html("'");
167         }
168         if (class) {
169                 html(" class='");
170                 html_attr(class);
171                 html("'");
172         }
173         html(" href='");
174         site_url(page, search, ofs);
175         html("'>");
176         html_txt(name);
177         html("</a>");
178 }
179
180 void cgit_index_link(char *name, char *title, char *class, char *pattern,
181                      int ofs)
182 {
183         site_link(NULL, name, title, class, pattern, ofs);
184 }
185
186 static char *repolink(char *title, char *class, char *page, char *head,
187                       char *path)
188 {
189         char *delim = "?";
190
191         html("<a");
192         if (title) {
193                 html(" title='");
194                 html_attr(title);
195                 html("'");
196         }
197         if (class) {
198                 html(" class='");
199                 html_attr(class);
200                 html("'");
201         }
202         html(" href='");
203         if (ctx.cfg.virtual_root) {
204                 html_attr(ctx.cfg.virtual_root);
205                 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
206                         html("/");
207                 html_attr(ctx.repo->url);
208                 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
209                         html("/");
210                 if (page) {
211                         html(page);
212                         html("/");
213                         if (path)
214                                 html_attr(path);
215                 }
216         } else {
217                 html(ctx.cfg.script_name);
218                 html("?url=");
219                 html_attr(ctx.repo->url);
220                 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
221                         html("/");
222                 if (page) {
223                         html(page);
224                         html("/");
225                         if (path)
226                                 html_attr(path);
227                 }
228                 delim = "&amp;";
229         }
230         if (head && strcmp(head, ctx.repo->defbranch)) {
231                 html(delim);
232                 html("h=");
233                 html_attr(head);
234                 delim = "&amp;";
235         }
236         return fmt("%s", delim);
237 }
238
239 static void reporevlink(char *page, char *name, char *title, char *class,
240                         char *head, char *rev, char *path)
241 {
242         char *delim;
243
244         delim = repolink(title, class, page, head, path);
245         if (rev && strcmp(rev, ctx.qry.head)) {
246                 html(delim);
247                 html("id=");
248                 html_attr(rev);
249         }
250         html("'>");
251         html_txt(name);
252         html("</a>");
253 }
254
255 void cgit_tree_link(char *name, char *title, char *class, char *head,
256                     char *rev, char *path)
257 {
258         reporevlink("tree", name, title, class, head, rev, path);
259 }
260
261 void cgit_plain_link(char *name, char *title, char *class, char *head,
262                      char *rev, char *path)
263 {
264         reporevlink("plain", name, title, class, head, rev, path);
265 }
266
267 void cgit_log_link(char *name, char *title, char *class, char *head,
268                    char *rev, char *path, int ofs, char *grep, char *pattern)
269 {
270         char *delim;
271
272         delim = repolink(title, class, "log", head, path);
273         if (rev && strcmp(rev, ctx.qry.head)) {
274                 html(delim);
275                 html("id=");
276                 html_attr(rev);
277                 delim = "&";
278         }
279         if (grep && pattern) {
280                 html(delim);
281                 html("qt=");
282                 html_attr(grep);
283                 delim = "&";
284                 html(delim);
285                 html("q=");
286                 html_attr(pattern);
287         }
288         if (ofs > 0) {
289                 html(delim);
290                 html("ofs=");
291                 htmlf("%d", ofs);
292         }
293         html("'>");
294         html_txt(name);
295         html("</a>");
296 }
297
298 void cgit_commit_link(char *name, char *title, char *class, char *head,
299                       char *rev)
300 {
301         if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
302                 name[ctx.cfg.max_msg_len] = '\0';
303                 name[ctx.cfg.max_msg_len - 1] = '.';
304                 name[ctx.cfg.max_msg_len - 2] = '.';
305                 name[ctx.cfg.max_msg_len - 3] = '.';
306         }
307         reporevlink("commit", name, title, class, head, rev, NULL);
308 }
309
310 void cgit_refs_link(char *name, char *title, char *class, char *head,
311                     char *rev, char *path)
312 {
313         reporevlink("refs", name, title, class, head, rev, path);
314 }
315
316 void cgit_snapshot_link(char *name, char *title, char *class, char *head,
317                         char *rev, char *archivename)
318 {
319         reporevlink("snapshot", name, title, class, head, rev, archivename);
320 }
321
322 void cgit_diff_link(char *name, char *title, char *class, char *head,
323                     char *new_rev, char *old_rev, char *path)
324 {
325         char *delim;
326
327         delim = repolink(title, class, "diff", head, path);
328         if (new_rev && strcmp(new_rev, ctx.qry.head)) {
329                 html(delim);
330                 html("id=");
331                 html_attr(new_rev);
332                 delim = "&amp;";
333         }
334         if (old_rev) {
335                 html(delim);
336                 html("id2=");
337                 html_attr(old_rev);
338         }
339         html("'>");
340         html_txt(name);
341         html("</a>");
342 }
343
344 void cgit_patch_link(char *name, char *title, char *class, char *head,
345                      char *rev)
346 {
347         reporevlink("patch", name, title, class, head, rev, NULL);
348 }
349
350 void cgit_object_link(struct object *obj)
351 {
352         char *page, *arg, *url;
353
354         if (obj->type == OBJ_COMMIT) {
355                 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
356                                  ctx.qry.head, sha1_to_hex(obj->sha1));
357                 return;
358         } else if (obj->type == OBJ_TREE) {
359                 page = "tree";
360                 arg = "id";
361         } else if (obj->type == OBJ_TAG) {
362                 page = "tag";
363                 arg = "id";
364         } else {
365                 page = "blob";
366                 arg = "id";
367         }
368
369         url = cgit_pageurl(ctx.qry.repo, page,
370                            fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
371         html_link_open(url, NULL, NULL);
372         htmlf("%s %s", typename(obj->type),
373               sha1_to_hex(obj->sha1));
374         html_link_close();
375 }
376
377 void cgit_print_date(time_t secs, char *format, int local_time)
378 {
379         char buf[64];
380         struct tm *time;
381
382         if (!secs)
383                 return;
384         if(local_time)
385                 time = localtime(&secs);
386         else
387                 time = gmtime(&secs);
388         strftime(buf, sizeof(buf)-1, format, time);
389         html_txt(buf);
390 }
391
392 void cgit_print_age(time_t t, time_t max_relative, char *format)
393 {
394         time_t now, secs;
395
396         if (!t)
397                 return;
398         time(&now);
399         secs = now - t;
400
401         if (secs > max_relative && max_relative >= 0) {
402                 cgit_print_date(t, format, ctx.cfg.local_time);
403                 return;
404         }
405
406         if (secs < TM_HOUR * 2) {
407                 htmlf("<span class='age-mins'>%.0f min.</span>",
408                       secs * 1.0 / TM_MIN);
409                 return;
410         }
411         if (secs < TM_DAY * 2) {
412                 htmlf("<span class='age-hours'>%.0f hours</span>",
413                       secs * 1.0 / TM_HOUR);
414                 return;
415         }
416         if (secs < TM_WEEK * 2) {
417                 htmlf("<span class='age-days'>%.0f days</span>",
418                       secs * 1.0 / TM_DAY);
419                 return;
420         }
421         if (secs < TM_MONTH * 2) {
422                 htmlf("<span class='age-weeks'>%.0f weeks</span>",
423                       secs * 1.0 / TM_WEEK);
424                 return;
425         }
426         if (secs < TM_YEAR * 2) {
427                 htmlf("<span class='age-months'>%.0f months</span>",
428                       secs * 1.0 / TM_MONTH);
429                 return;
430         }
431         htmlf("<span class='age-years'>%.0f years</span>",
432               secs * 1.0 / TM_YEAR);
433 }
434
435 void cgit_print_http_headers(struct cgit_context *ctx)
436 {
437         if (ctx->page.mimetype && ctx->page.charset)
438                 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
439                       ctx->page.charset);
440         else if (ctx->page.mimetype)
441                 htmlf("Content-Type: %s\n", ctx->page.mimetype);
442         if (ctx->page.size)
443                 htmlf("Content-Length: %ld\n", ctx->page.size);
444         if (ctx->page.filename)
445                 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
446                       ctx->page.filename);
447         htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
448         htmlf("Expires: %s\n", http_date(ctx->page.expires));
449         html("\n");
450 }
451
452 void cgit_print_docstart(struct cgit_context *ctx)
453 {
454         char *host = cgit_hosturl();
455         html(cgit_doctype);
456         html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
457         html("<head>\n");
458         html("<title>");
459         html_txt(ctx->page.title);
460         html("</title>\n");
461         htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
462         if (ctx->cfg.robots && *ctx->cfg.robots)
463                 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
464         html("<link rel='stylesheet' type='text/css' href='");
465         html_attr(ctx->cfg.css);
466         html("'/>\n");
467         if (ctx->cfg.favicon) {
468                 html("<link rel='shortcut icon' href='");
469                 html_attr(ctx->cfg.favicon);
470                 html("'/>\n");
471         }
472         if (host && ctx->repo) {
473                 html("<link rel='alternate' title='Atom feed' href='http://");
474                 html_attr(cgit_hosturl());
475                 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path,
476                                        fmt("h=%s", ctx->qry.head)));
477                 html("' type='application/atom+xml'/>");
478         }
479         html("</head>\n");
480         html("<body>\n");
481 }
482
483 void cgit_print_docend()
484 {
485         html("</div>");
486         if (ctx.cfg.footer)
487                 html_include(ctx.cfg.footer);
488         else {
489                 html("<div class='footer'>generated ");
490                 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
491                 htmlf(" by cgit %s", cgit_version);
492                 html("</div>\n");
493         }
494         html("</body>\n</html>\n");
495 }
496
497 int print_branch_option(const char *refname, const unsigned char *sha1,
498                         int flags, void *cb_data)
499 {
500         char *name = (char *)refname;
501         html_option(name, name, ctx.qry.head);
502         return 0;
503 }
504
505 int print_archive_ref(const char *refname, const unsigned char *sha1,
506                        int flags, void *cb_data)
507 {
508         struct tag *tag;
509         struct taginfo *info;
510         struct object *obj;
511         char buf[256], *url;
512         unsigned char fileid[20];
513         int *header = (int *)cb_data;
514
515         if (prefixcmp(refname, "refs/archives"))
516                 return 0;
517         strncpy(buf, refname+14, sizeof(buf));
518         obj = parse_object(sha1);
519         if (!obj)
520                 return 1;
521         if (obj->type == OBJ_TAG) {
522                 tag = lookup_tag(sha1);
523                 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
524                         return 0;
525                 hashcpy(fileid, tag->tagged->sha1);
526         } else if (obj->type != OBJ_BLOB) {
527                 return 0;
528         } else {
529                 hashcpy(fileid, sha1);
530         }
531         if (!*header) {
532                 html("<h1>download</h1>\n");
533                 *header = 1;
534         }
535         url = cgit_pageurl(ctx.qry.repo, "blob",
536                            fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
537                                buf));
538         html_link_open(url, NULL, "menu");
539         html_txt(strlpart(buf, 20));
540         html_link_close();
541         return 0;
542 }
543
544 void add_hidden_formfields(int incl_head, int incl_search, char *page)
545 {
546         char *url;
547
548         if (!ctx.cfg.virtual_root) {
549                 url = fmt("%s/%s", ctx.qry.repo, page);
550                 if (ctx.qry.path)
551                         url = fmt("%s/%s", url, ctx.qry.path);
552                 html_hidden("url", url);
553         }
554
555         if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
556             strcmp(ctx.qry.head, ctx.repo->defbranch))
557                 html_hidden("h", ctx.qry.head);
558
559         if (ctx.qry.sha1)
560                 html_hidden("id", ctx.qry.sha1);
561         if (ctx.qry.sha2)
562                 html_hidden("id2", ctx.qry.sha2);
563
564         if (incl_search) {
565                 if (ctx.qry.grep)
566                         html_hidden("qt", ctx.qry.grep);
567                 if (ctx.qry.search)
568                         html_hidden("q", ctx.qry.search);
569         }
570 }
571
572 char *hc(struct cgit_cmd *cmd, const char *page)
573 {
574         return (strcmp(cmd->name, page) ? NULL : "active");
575 }
576
577 void cgit_print_pageheader(struct cgit_context *ctx)
578 {
579         struct cgit_cmd *cmd = cgit_get_cmd(ctx);
580
581         html("<table id='header'>\n");
582         html("<tr>\n");
583         html("<td class='logo' rowspan='2'><a href='");
584         if (ctx->cfg.logo_link)
585                 html_attr(ctx->cfg.logo_link);
586         else
587                 html_attr(cgit_rooturl());
588         html("'><img src='");
589         html_attr(ctx->cfg.logo);
590         html("' alt='cgit logo'/></a></td>\n");
591
592         html("<td class='main'>");
593         if (ctx->repo) {
594                 cgit_index_link("index", NULL, NULL, NULL, 0);
595                 html(" : ");
596                 reporevlink(NULL, ctx->repo->name, NULL, hc(cmd, "summary"),
597                             ctx->qry.head, NULL, NULL);
598                 html("</td><td class='form'>");
599                 html("<form method='get' action=''>\n");
600                 add_hidden_formfields(0, 1, ctx->qry.page);
601                 html("<select name='h' onchange='this.form.submit();'>\n");
602                 for_each_branch_ref(print_branch_option, ctx->qry.head);
603                 html("</select> ");
604                 html("<input type='submit' name='' value='switch'/>");
605                 html("</form>");
606         } else
607                 html_txt(ctx->cfg.root_title);
608         html("</td></tr>\n");
609
610         html("<tr><td class='sub'>");
611         if (ctx->repo) {
612                 html_txt(ctx->repo->desc);
613                 html("</td><td class='sub right'>");
614                 html_txt(ctx->repo->owner);
615         } else {
616                 if (ctx->cfg.root_desc)
617                         html_txt(ctx->cfg.root_desc);
618                 else if (ctx->cfg.index_info)
619                         html_include(ctx->cfg.index_info);
620         }
621         html("</td></tr></table>\n");
622
623         html("<table class='tabs'><tr><td>\n");
624         if (ctx->repo) {
625                 reporevlink(NULL, "summary", NULL, hc(cmd, "summary"),
626                             ctx->qry.head, NULL, NULL);
627                 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head,
628                                ctx->qry.sha1, NULL);
629                 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head,
630                               NULL, NULL, 0, NULL, NULL);
631                 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head,
632                                ctx->qry.sha1, NULL);
633                 cgit_commit_link("commit", NULL, hc(cmd, "commit"),
634                                  ctx->qry.head, ctx->qry.sha1);
635                 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head,
636                                ctx->qry.sha1, ctx->qry.sha2, NULL);
637                 if (ctx->repo->readme)
638                         reporevlink("about", "about", NULL,
639                                     hc(cmd, "about"), ctx->qry.head, NULL,
640                                     NULL);
641                 html("</td><td class='form'>");
642                 html("<form class='right' method='get' action='");
643                 if (ctx->cfg.virtual_root)
644                         html_attr(cgit_fileurl(ctx->qry.repo, "log",
645                                                ctx->qry.path, NULL));
646                 html("'>\n");
647                 add_hidden_formfields(1, 0, "log");
648                 html("<select name='qt'>\n");
649                 html_option("grep", "log msg", ctx->qry.grep);
650                 html_option("author", "author", ctx->qry.grep);
651                 html_option("committer", "committer", ctx->qry.grep);
652                 html("</select>\n");
653                 html("<input class='txt' type='text' size='10' name='q' value='");
654                 html_attr(ctx->qry.search);
655                 html("'/>\n");
656                 html("<input type='submit' value='search'/>\n");
657                 html("</form>\n");
658         } else {
659                 site_link(NULL, "index", NULL, hc(cmd, "repolist"), NULL, 0);
660                 if (ctx->cfg.root_readme)
661                         site_link("about", "about", NULL, hc(cmd, "about"),
662                                   NULL, 0);
663                 html("</td><td class='form'>");
664                 html("<form method='get' action='");
665                 html_attr(cgit_rooturl());
666                 html("'>\n");
667                 html("<input type='text' name='q' size='10' value='");
668                 html_attr(ctx->qry.search);
669                 html("'/>\n");
670                 html("<input type='submit' value='search'/>\n");
671                 html("</form>");
672         }
673         html("</td></tr></table>\n");
674         html("<div class='content'>");
675 }
676
677 void cgit_print_filemode(unsigned short mode)
678 {
679         if (S_ISDIR(mode))
680                 html("d");
681         else if (S_ISLNK(mode))
682                 html("l");
683         else if (S_ISGITLINK(mode))
684                 html("m");
685         else
686                 html("-");
687         html_fileperm(mode >> 6);
688         html_fileperm(mode >> 3);
689         html_fileperm(mode);
690 }
691
692 void cgit_print_snapshot_links(const char *repo, const char *head,
693                                const char *hex, int snapshots)
694 {
695         const struct cgit_snapshot_format* f;
696         char *filename;
697
698         for (f = cgit_snapshot_formats; f->suffix; f++) {
699                 if (!(snapshots & f->bit))
700                         continue;
701                 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
702                                f->suffix);
703                 cgit_snapshot_link(filename, NULL, NULL, (char *)head,
704                                    (char *)hex, filename);
705                 html("<br/>");
706         }
707 }