-rw-r--r-- | ui-shared.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/ui-shared.c b/ui-shared.c index 1c1415e..3e378a4 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -1,179 +1,203 @@ | |||
1 | /* ui-shared.c: common web output functions | 1 | /* ui-shared.c: common web output functions |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | 10 | ||
11 | const char cgit_doctype[] = | 11 | const char cgit_doctype[] = |
12 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | 12 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" |
13 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | 13 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
14 | 14 | ||
15 | static char *http_date(time_t t) | 15 | static char *http_date(time_t t) |
16 | { | 16 | { |
17 | static char day[][4] = | 17 | static char day[][4] = |
18 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | 18 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
19 | static char month[][4] = | 19 | static char month[][4] = |
20 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | 20 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
21 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; | 21 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; |
22 | struct tm *tm = gmtime(&t); | 22 | struct tm *tm = gmtime(&t); |
23 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], | 23 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], |
24 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, | 24 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, |
25 | tm->tm_hour, tm->tm_min, tm->tm_sec); | 25 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
26 | } | 26 | } |
27 | 27 | ||
28 | static long ttl_seconds(long ttl) | 28 | static long ttl_seconds(long ttl) |
29 | { | 29 | { |
30 | if (ttl<0) | 30 | if (ttl<0) |
31 | return 60 * 60 * 24 * 365; | 31 | return 60 * 60 * 24 * 365; |
32 | else | 32 | else |
33 | return ttl * 60; | 33 | return ttl * 60; |
34 | } | 34 | } |
35 | 35 | ||
36 | void cgit_print_error(char *msg) | 36 | void cgit_print_error(char *msg) |
37 | { | 37 | { |
38 | html("<div class='error'>"); | 38 | html("<div class='error'>"); |
39 | html_txt(msg); | 39 | html_txt(msg); |
40 | html("</div>\n"); | 40 | html("</div>\n"); |
41 | } | 41 | } |
42 | 42 | ||
43 | char *cgit_rooturl() | 43 | char *cgit_rooturl() |
44 | { | 44 | { |
45 | if (cgit_virtual_root) | 45 | if (cgit_virtual_root) |
46 | return fmt("%s/", cgit_virtual_root); | 46 | return fmt("%s/", cgit_virtual_root); |
47 | else | 47 | else |
48 | return cgit_script_name; | 48 | return cgit_script_name; |
49 | } | 49 | } |
50 | 50 | ||
51 | char *cgit_repourl(const char *reponame) | 51 | char *cgit_repourl(const char *reponame) |
52 | { | 52 | { |
53 | if (cgit_virtual_root) { | 53 | if (cgit_virtual_root) { |
54 | return fmt("%s/%s/", cgit_virtual_root, reponame); | 54 | return fmt("%s/%s/", cgit_virtual_root, reponame); |
55 | } else { | 55 | } else { |
56 | return fmt("?r=%s", reponame); | 56 | return fmt("?r=%s", reponame); |
57 | } | 57 | } |
58 | } | 58 | } |
59 | 59 | ||
60 | char *cgit_fileurl(const char *reponame, const char *pagename, | 60 | char *cgit_fileurl(const char *reponame, const char *pagename, |
61 | const char *filename, const char *query) | 61 | const char *filename, const char *query) |
62 | { | 62 | { |
63 | if (cgit_virtual_root) { | 63 | if (cgit_virtual_root) { |
64 | if (query) | 64 | if (query) |
65 | return fmt("%s/%s/%s/%s?%s", cgit_virtual_root, reponame, | 65 | return fmt("%s/%s/%s/%s?%s", cgit_virtual_root, reponame, |
66 | pagename, filename?filename:"", query); | 66 | pagename, filename?filename:"", query); |
67 | else | 67 | else |
68 | return fmt("%s/%s/%s/", cgit_virtual_root, reponame, | 68 | return fmt("%s/%s/%s/", cgit_virtual_root, reponame, |
69 | pagename); | 69 | pagename); |
70 | } else { | 70 | } else { |
71 | if (query) | 71 | if (query) |
72 | return fmt("?r=%s&p=%s&%s", reponame, pagename, query); | 72 | return fmt("?r=%s&p=%s&%s", reponame, pagename, query); |
73 | else | 73 | else |
74 | return fmt("?r=%s&p=%s", reponame, pagename); | 74 | return fmt("?r=%s&p=%s", reponame, pagename); |
75 | } | 75 | } |
76 | } | 76 | } |
77 | 77 | ||
78 | char *cgit_pageurl(const char *reponame, const char *pagename, | 78 | char *cgit_pageurl(const char *reponame, const char *pagename, |
79 | const char *query) | 79 | const char *query) |
80 | { | 80 | { |
81 | return cgit_fileurl(reponame,pagename,0,query); | 81 | return cgit_fileurl(reponame,pagename,0,query); |
82 | } | 82 | } |
83 | 83 | ||
84 | const char *cgit_repobasename(const char *reponame) | ||
85 | { | ||
86 | /* I assume we don't need to store more than one repo basename */ | ||
87 | static char rvbuf[1024]; | ||
88 | int p; | ||
89 | const char *rv; | ||
90 | strncpy(rvbuf,reponame,sizeof(rvbuf)); | ||
91 | if(rvbuf[sizeof(rvbuf)-1]) | ||
92 | die("cgit_repobasename: truncated repository name '%s'", reponame); | ||
93 | p = strlen(rvbuf)-1; | ||
94 | /* strip trailing slashes */ | ||
95 | while(p && rvbuf[p]=='/') rvbuf[p--]=0; | ||
96 | /* strip trailing .git */ | ||
97 | if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { | ||
98 | p -= 3; rvbuf[p--] = 0; | ||
99 | } | ||
100 | /* strip more trailing slashes if any */ | ||
101 | while( p && rvbuf[p]=='/') rvbuf[p--]=0; | ||
102 | /* find last slash in the remaining string */ | ||
103 | rv = strrchr(rvbuf,'/'); | ||
104 | if(rv) | ||
105 | return ++rv; | ||
106 | return rvbuf; | ||
107 | } | ||
84 | 108 | ||
85 | char *cgit_currurl() | 109 | char *cgit_currurl() |
86 | { | 110 | { |
87 | if (!cgit_virtual_root) | 111 | if (!cgit_virtual_root) |
88 | return cgit_script_name; | 112 | return cgit_script_name; |
89 | else if (cgit_query_page) | 113 | else if (cgit_query_page) |
90 | return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); | 114 | return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); |
91 | else if (cgit_query_repo) | 115 | else if (cgit_query_repo) |
92 | return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); | 116 | return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); |
93 | else | 117 | else |
94 | return fmt("%s/", cgit_virtual_root); | 118 | return fmt("%s/", cgit_virtual_root); |
95 | } | 119 | } |
96 | 120 | ||
97 | static char *repolink(char *title, char *class, char *page, char *head, | 121 | static char *repolink(char *title, char *class, char *page, char *head, |
98 | char *path) | 122 | char *path) |
99 | { | 123 | { |
100 | char *delim = "?"; | 124 | char *delim = "?"; |
101 | 125 | ||
102 | html("<a"); | 126 | html("<a"); |
103 | if (title) { | 127 | if (title) { |
104 | html(" title='"); | 128 | html(" title='"); |
105 | html_attr(title); | 129 | html_attr(title); |
106 | html("'"); | 130 | html("'"); |
107 | } | 131 | } |
108 | if (class) { | 132 | if (class) { |
109 | html(" class='"); | 133 | html(" class='"); |
110 | html_attr(class); | 134 | html_attr(class); |
111 | html("'"); | 135 | html("'"); |
112 | } | 136 | } |
113 | html(" href='"); | 137 | html(" href='"); |
114 | if (cgit_virtual_root) { | 138 | if (cgit_virtual_root) { |
115 | html_attr(cgit_virtual_root); | 139 | html_attr(cgit_virtual_root); |
116 | if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/') | 140 | if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/') |
117 | html("/"); | 141 | html("/"); |
118 | html_attr(cgit_repo->url); | 142 | html_attr(cgit_repo->url); |
119 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') | 143 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') |
120 | html("/"); | 144 | html("/"); |
121 | if (page) { | 145 | if (page) { |
122 | html(page); | 146 | html(page); |
123 | html("/"); | 147 | html("/"); |
124 | if (path) | 148 | if (path) |
125 | html_attr(path); | 149 | html_attr(path); |
126 | } | 150 | } |
127 | } else { | 151 | } else { |
128 | html(cgit_script_name); | 152 | html(cgit_script_name); |
129 | html("?url="); | 153 | html("?url="); |
130 | html_attr(cgit_repo->url); | 154 | html_attr(cgit_repo->url); |
131 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') | 155 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') |
132 | html("/"); | 156 | html("/"); |
133 | if (page) { | 157 | if (page) { |
134 | html(page); | 158 | html(page); |
135 | html("/"); | 159 | html("/"); |
136 | if (path) | 160 | if (path) |
137 | html_attr(path); | 161 | html_attr(path); |
138 | } | 162 | } |
139 | delim = "&"; | 163 | delim = "&"; |
140 | } | 164 | } |
141 | if (head && strcmp(head, cgit_repo->defbranch)) { | 165 | if (head && strcmp(head, cgit_repo->defbranch)) { |
142 | html(delim); | 166 | html(delim); |
143 | html("h="); | 167 | html("h="); |
144 | html_attr(head); | 168 | html_attr(head); |
145 | delim = "&"; | 169 | delim = "&"; |
146 | } | 170 | } |
147 | return fmt("%s", delim); | 171 | return fmt("%s", delim); |
148 | } | 172 | } |
149 | 173 | ||
150 | static void reporevlink(char *page, char *name, char *title, char *class, | 174 | static void reporevlink(char *page, char *name, char *title, char *class, |
151 | char *head, char *rev, char *path) | 175 | char *head, char *rev, char *path) |
152 | { | 176 | { |
153 | char *delim; | 177 | char *delim; |
154 | 178 | ||
155 | delim = repolink(title, class, page, head, path); | 179 | delim = repolink(title, class, page, head, path); |
156 | if (rev && strcmp(rev, cgit_query_head)) { | 180 | if (rev && strcmp(rev, cgit_query_head)) { |
157 | html(delim); | 181 | html(delim); |
158 | html("id="); | 182 | html("id="); |
159 | html_attr(rev); | 183 | html_attr(rev); |
160 | } | 184 | } |
161 | html("'>"); | 185 | html("'>"); |
162 | html_txt(name); | 186 | html_txt(name); |
163 | html("</a>"); | 187 | html("</a>"); |
164 | } | 188 | } |
165 | 189 | ||
166 | void cgit_tree_link(char *name, char *title, char *class, char *head, | 190 | void cgit_tree_link(char *name, char *title, char *class, char *head, |
167 | char *rev, char *path) | 191 | char *rev, char *path) |
168 | { | 192 | { |
169 | reporevlink("tree", name, title, class, head, rev, path); | 193 | reporevlink("tree", name, title, class, head, rev, path); |
170 | } | 194 | } |
171 | 195 | ||
172 | void cgit_log_link(char *name, char *title, char *class, char *head, | 196 | void cgit_log_link(char *name, char *title, char *class, char *head, |
173 | char *rev, char *path, int ofs) | 197 | char *rev, char *path, int ofs) |
174 | { | 198 | { |
175 | char *delim; | 199 | char *delim; |
176 | 200 | ||
177 | delim = repolink(title, class, "log", head, path); | 201 | delim = repolink(title, class, "log", head, path); |
178 | if (rev && strcmp(rev, cgit_query_head)) { | 202 | if (rev && strcmp(rev, cgit_query_head)) { |
179 | html(delim); | 203 | html(delim); |