diff options
author | Lars Hjemli <hjemli@gmail.com> | 2008-04-09 04:29:21 (JST) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-04-09 04:29:21 (JST) |
commit | 23296ad648c0e2a9e3cf40a3de322b10ad25cce3 (patch) | |
tree | 136493d8228b0ff4971feb06b0e8aee296367b00 /html.c | |
parent | e2a44cf0923398396b7a321d5ce894ad3bf6f580 (diff) | |
parent | c6f747649ace1a92ed5dfaae9cc1ea3affe0bf51 (diff) | |
download | cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.zip cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.tar.gz |
Merge branch 'lh/cleanup'
* lh/cleanup: (21 commits)
Reset ctx.repo to NULL when the config parser is finished
Move cgit_parse_query() from parsing.c to html.c as http_parse_querystring()
Move function for configfile parsing into configfile.[ch]
Add cache.h
Remove global and obsolete cgit_cmd
Makefile: copy the QUIET constructs from the Makefile in git.git
Move cgit_version from shared.c to cgit.c
Makefile: autobuild dependency rules
Initial Makefile cleanup
Move non-generic functions from shared.c to cgit.c
Add ui-shared.h
Add separate header-files for each page/view
Refactor snapshot support
Add command dispatcher
Remove obsolete cacheitem parameter to ui-functions
Add struct cgit_page to cgit_context
Introduce html.h
Improve initialization of git directory
Move cgit_repo into cgit_context
Add all config variables into struct cgit_context
...
Diffstat (limited to 'html.c')
-rw-r--r-- | html.c | 95 |
1 files changed, 76 insertions, 19 deletions
@@ -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 | |||
15 | int htmlfd = STDOUT_FILENO; | ||
10 | 16 | ||
11 | char *fmt(const char *format, ...) | 17 | char *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 | ||
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; |
@@ -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 | |||
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 | } | ||