aboutsummaryrefslogtreecommitdiffstats
path: root/html.c
diff options
context:
space:
mode:
Diffstat (limited to 'html.c')
-rw-r--r--html.c95
1 files changed, 76 insertions, 19 deletions
diff --git a/html.c b/html.c
index 339bf00..937b5e7 100644
--- a/html.c
+++ b/html.c
@@ -6,7 +6,13 @@
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
15int htmlfd = STDOUT_FILENO;
10 16
11char *fmt(const char *format, ...) 17char *fmt(const char *format, ...)
12{ 18{
@@ -21,8 +27,10 @@ char *fmt(const char *format, ...)
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
@@ -150,25 +158,10 @@ void html_link_close(void)
150 158
151void html_fileperm(unsigned short mode) 159void 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
157void 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
172int html_include(const char *filename) 165int html_include(const char *filename)
173{ 166{
174 FILE *f; 167 FILE *f;
@@ -182,3 +175,67 @@ int html_include(const char *filename)
182 fclose(f); 175 fclose(f);
183 return 0; 176 return 0;
184} 177}
178
179int 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
191char *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
210int 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}