|
diff --git a/html.c b/html.c index 339bf00..937b5e7 100644 --- a/ html.c+++ b/ html.c |
|
@@ -1,41 +1,49 @@ |
1 | /* html.c: helper functions for html output |
1 | /* html.c: helper functions for html output |
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 <unistd.h> |
| |
10 | #include <stdio.h> |
| |
11 | #include <stdlib.h> |
| |
12 | #include <stdarg.h> |
| |
13 | #include <string.h> |
| |
14 | |
| |
15 | int htmlfd = STDOUT_FILENO; |
10 | |
16 | |
11 | char *fmt(const char *format, ...) |
17 | char *fmt(const char *format, ...) |
12 | { |
18 | { |
13 | static char buf[8][1024]; |
19 | static char buf[8][1024]; |
14 | static int bufidx; |
20 | static int bufidx; |
15 | int len; |
21 | int len; |
16 | va_list args; |
22 | va_list args; |
17 | |
23 | |
18 | bufidx++; |
24 | bufidx++; |
19 | bufidx &= 7; |
25 | bufidx &= 7; |
20 | |
26 | |
21 | va_start(args, format); |
27 | va_start(args, format); |
22 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); |
28 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); |
23 | va_end(args); |
29 | va_end(args); |
24 | if (len>sizeof(buf[bufidx])) |
30 | if (len>sizeof(buf[bufidx])) { |
25 | die("[html.c] string truncated: %s", format); |
31 | fprintf(stderr, "[html.c] string truncated: %s\n", format); |
| |
32 | exit(1); |
| |
33 | } |
26 | return buf[bufidx]; |
34 | return buf[bufidx]; |
27 | } |
35 | } |
28 | |
36 | |
29 | void html(const char *txt) |
37 | void html(const char *txt) |
30 | { |
38 | { |
31 | write(htmlfd, txt, strlen(txt)); |
39 | write(htmlfd, txt, strlen(txt)); |
32 | } |
40 | } |
33 | |
41 | |
34 | void htmlf(const char *format, ...) |
42 | void htmlf(const char *format, ...) |
35 | { |
43 | { |
36 | static char buf[65536]; |
44 | static char buf[65536]; |
37 | va_list args; |
45 | va_list args; |
38 | |
46 | |
39 | va_start(args, format); |
47 | va_start(args, format); |
40 | vsnprintf(buf, sizeof(buf), format, args); |
48 | vsnprintf(buf, sizeof(buf), format, args); |
41 | va_end(args); |
49 | va_end(args); |
@@ -137,48 +145,97 @@ void html_link_open(char *url, char *title, char *class) |
137 | html_attr(title); |
145 | html_attr(title); |
138 | } |
146 | } |
139 | if (class) { |
147 | if (class) { |
140 | html("' class='"); |
148 | html("' class='"); |
141 | html_attr(class); |
149 | html_attr(class); |
142 | } |
150 | } |
143 | html("'>"); |
151 | html("'>"); |
144 | } |
152 | } |
145 | |
153 | |
146 | void html_link_close(void) |
154 | void html_link_close(void) |
147 | { |
155 | { |
148 | html("</a>"); |
156 | html("</a>"); |
149 | } |
157 | } |
150 | |
158 | |
151 | void html_fileperm(unsigned short mode) |
159 | void html_fileperm(unsigned short mode) |
152 | { |
160 | { |
153 | htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), |
161 | htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), |
154 | (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); |
162 | (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); |
155 | } |
163 | } |
156 | |
164 | |
157 | void html_filemode(unsigned short mode) |
| |
158 | { |
| |
159 | if (S_ISDIR(mode)) |
| |
160 | html("d"); |
| |
161 | else if (S_ISLNK(mode)) |
| |
162 | html("l"); |
| |
163 | else if (S_ISGITLINK(mode)) |
| |
164 | html("m"); |
| |
165 | else |
| |
166 | html("-"); |
| |
167 | html_fileperm(mode >> 6); |
| |
168 | html_fileperm(mode >> 3); |
| |
169 | html_fileperm(mode); |
| |
170 | } |
| |
171 | |
| |
172 | int html_include(const char *filename) |
165 | int html_include(const char *filename) |
173 | { |
166 | { |
174 | FILE *f; |
167 | FILE *f; |
175 | char buf[4096]; |
168 | char buf[4096]; |
176 | size_t len; |
169 | size_t len; |
177 | |
170 | |
178 | if (!(f = fopen(filename, "r"))) |
171 | if (!(f = fopen(filename, "r"))) |
179 | return -1; |
172 | return -1; |
180 | while((len = fread(buf, 1, 4096, f)) > 0) |
173 | while((len = fread(buf, 1, 4096, f)) > 0) |
181 | write(htmlfd, buf, len); |
174 | write(htmlfd, buf, len); |
182 | fclose(f); |
175 | fclose(f); |
183 | return 0; |
176 | return 0; |
184 | } |
177 | } |
| |
178 | |
| |
179 | int hextoint(char c) |
| |
180 | { |
| |
181 | if (c >= 'a' && c <= 'f') |
| |
182 | return 10 + c - 'a'; |
| |
183 | else if (c >= 'A' && c <= 'F') |
| |
184 | return 10 + c - 'A'; |
| |
185 | else if (c >= '0' && c <= '9') |
| |
186 | return c - '0'; |
| |
187 | else |
| |
188 | return -1; |
| |
189 | } |
| |
190 | |
| |
191 | char *convert_query_hexchar(char *txt) |
| |
192 | { |
| |
193 | int d1, d2; |
| |
194 | if (strlen(txt) < 3) { |
| |
195 | *txt = '\0'; |
| |
196 | return txt-1; |
| |
197 | } |
| |
198 | d1 = hextoint(*(txt+1)); |
| |
199 | d2 = hextoint(*(txt+2)); |
| |
200 | if (d1<0 || d2<0) { |
| |
201 | strcpy(txt, txt+3); |
| |
202 | return txt-1; |
| |
203 | } else { |
| |
204 | *txt = d1 * 16 + d2; |
| |
205 | strcpy(txt+1, txt+3); |
| |
206 | return txt; |
| |
207 | } |
| |
208 | } |
| |
209 | |
| |
210 | int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)) |
| |
211 | { |
| |
212 | char *t, *value = NULL, c; |
| |
213 | |
| |
214 | if (!txt) |
| |
215 | return 0; |
| |
216 | |
| |
217 | t = txt = strdup(txt); |
| |
218 | if (t == NULL) { |
| |
219 | printf("Out of memory\n"); |
| |
220 | exit(1); |
| |
221 | } |
| |
222 | while((c=*t) != '\0') { |
| |
223 | if (c=='=') { |
| |
224 | *t = '\0'; |
| |
225 | value = t+1; |
| |
226 | } else if (c=='+') { |
| |
227 | *t = ' '; |
| |
228 | } else if (c=='%') { |
| |
229 | t = convert_query_hexchar(t); |
| |
230 | } else if (c=='&') { |
| |
231 | *t = '\0'; |
| |
232 | (*fn)(txt, value); |
| |
233 | txt = t+1; |
| |
234 | value = NULL; |
| |
235 | } |
| |
236 | t++; |
| |
237 | } |
| |
238 | if (t!=txt) |
| |
239 | (*fn)(txt, value); |
| |
240 | return 0; |
| |
241 | } |
|