|
diff --git a/html.c b/html.c index bddb04d..1237076 100644 --- a/ html.c+++ b/ html.c |
|
@@ -1,245 +1,252 @@ |
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 <unistd.h> |
9 | #include <unistd.h> |
10 | #include <stdio.h> |
10 | #include <stdio.h> |
11 | #include <stdlib.h> |
11 | #include <stdlib.h> |
12 | #include <stdarg.h> |
12 | #include <stdarg.h> |
13 | #include <string.h> |
13 | #include <string.h> |
14 | #include <errno.h> |
14 | #include <errno.h> |
15 | |
15 | |
16 | int htmlfd = STDOUT_FILENO; |
16 | int htmlfd = STDOUT_FILENO; |
17 | |
17 | |
18 | char *fmt(const char *format, ...) |
18 | char *fmt(const char *format, ...) |
19 | { |
19 | { |
20 | static char buf[8][1024]; |
20 | static char buf[8][1024]; |
21 | static int bufidx; |
21 | static int bufidx; |
22 | int len; |
22 | int len; |
23 | va_list args; |
23 | va_list args; |
24 | |
24 | |
25 | bufidx++; |
25 | bufidx++; |
26 | bufidx &= 7; |
26 | bufidx &= 7; |
27 | |
27 | |
28 | va_start(args, format); |
28 | va_start(args, format); |
29 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); |
29 | len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args); |
30 | va_end(args); |
30 | va_end(args); |
31 | if (len>sizeof(buf[bufidx])) { |
31 | if (len>sizeof(buf[bufidx])) { |
32 | fprintf(stderr, "[html.c] string truncated: %s\n", format); |
32 | fprintf(stderr, "[html.c] string truncated: %s\n", format); |
33 | exit(1); |
33 | exit(1); |
34 | } |
34 | } |
35 | return buf[bufidx]; |
35 | return buf[bufidx]; |
36 | } |
36 | } |
37 | |
37 | |
38 | void html(const char *txt) |
38 | void html(const char *txt) |
39 | { |
39 | { |
40 | write(htmlfd, txt, strlen(txt)); |
40 | write(htmlfd, txt, strlen(txt)); |
41 | } |
41 | } |
42 | |
42 | |
43 | void htmlf(const char *format, ...) |
43 | void htmlf(const char *format, ...) |
44 | { |
44 | { |
45 | static char buf[65536]; |
45 | static char buf[65536]; |
46 | va_list args; |
46 | va_list args; |
47 | |
47 | |
48 | va_start(args, format); |
48 | va_start(args, format); |
49 | vsnprintf(buf, sizeof(buf), format, args); |
49 | vsnprintf(buf, sizeof(buf), format, args); |
50 | va_end(args); |
50 | va_end(args); |
51 | html(buf); |
51 | html(buf); |
52 | } |
52 | } |
53 | |
53 | |
| |
54 | void html_status(int code, int more_headers) |
| |
55 | { |
| |
56 | htmlf("Status: %d\n", code); |
| |
57 | if (!more_headers) |
| |
58 | html("\n"); |
| |
59 | } |
| |
60 | |
54 | void html_txt(char *txt) |
61 | void html_txt(char *txt) |
55 | { |
62 | { |
56 | char *t = txt; |
63 | char *t = txt; |
57 | while(t && *t){ |
64 | while(t && *t){ |
58 | int c = *t; |
65 | int c = *t; |
59 | if (c=='<' || c=='>' || c=='&') { |
66 | if (c=='<' || c=='>' || c=='&') { |
60 | write(htmlfd, txt, t - txt); |
67 | write(htmlfd, txt, t - txt); |
61 | if (c=='>') |
68 | if (c=='>') |
62 | html(">"); |
69 | html(">"); |
63 | else if (c=='<') |
70 | else if (c=='<') |
64 | html("<"); |
71 | html("<"); |
65 | else if (c=='&') |
72 | else if (c=='&') |
66 | html("&"); |
73 | html("&"); |
67 | txt = t+1; |
74 | txt = t+1; |
68 | } |
75 | } |
69 | t++; |
76 | t++; |
70 | } |
77 | } |
71 | if (t!=txt) |
78 | if (t!=txt) |
72 | html(txt); |
79 | html(txt); |
73 | } |
80 | } |
74 | |
81 | |
75 | void html_ntxt(int len, char *txt) |
82 | void html_ntxt(int len, char *txt) |
76 | { |
83 | { |
77 | char *t = txt; |
84 | char *t = txt; |
78 | while(t && *t && len--){ |
85 | while(t && *t && len--){ |
79 | int c = *t; |
86 | int c = *t; |
80 | if (c=='<' || c=='>' || c=='&') { |
87 | if (c=='<' || c=='>' || c=='&') { |
81 | write(htmlfd, txt, t - txt); |
88 | write(htmlfd, txt, t - txt); |
82 | if (c=='>') |
89 | if (c=='>') |
83 | html(">"); |
90 | html(">"); |
84 | else if (c=='<') |
91 | else if (c=='<') |
85 | html("<"); |
92 | html("<"); |
86 | else if (c=='&') |
93 | else if (c=='&') |
87 | html("&"); |
94 | html("&"); |
88 | txt = t+1; |
95 | txt = t+1; |
89 | } |
96 | } |
90 | t++; |
97 | t++; |
91 | } |
98 | } |
92 | if (t!=txt) |
99 | if (t!=txt) |
93 | write(htmlfd, txt, t - txt); |
100 | write(htmlfd, txt, t - txt); |
94 | if (len<0) |
101 | if (len<0) |
95 | html("..."); |
102 | html("..."); |
96 | } |
103 | } |
97 | |
104 | |
98 | void html_attr(char *txt) |
105 | void html_attr(char *txt) |
99 | { |
106 | { |
100 | char *t = txt; |
107 | char *t = txt; |
101 | while(t && *t){ |
108 | while(t && *t){ |
102 | int c = *t; |
109 | int c = *t; |
103 | if (c=='<' || c=='>' || c=='\'') { |
110 | if (c=='<' || c=='>' || c=='\'') { |
104 | write(htmlfd, txt, t - txt); |
111 | write(htmlfd, txt, t - txt); |
105 | if (c=='>') |
112 | if (c=='>') |
106 | html(">"); |
113 | html(">"); |
107 | else if (c=='<') |
114 | else if (c=='<') |
108 | html("<"); |
115 | html("<"); |
109 | else if (c=='\'') |
116 | else if (c=='\'') |
110 | html(""e;"); |
117 | html(""e;"); |
111 | txt = t+1; |
118 | txt = t+1; |
112 | } |
119 | } |
113 | t++; |
120 | t++; |
114 | } |
121 | } |
115 | if (t!=txt) |
122 | if (t!=txt) |
116 | html(txt); |
123 | html(txt); |
117 | } |
124 | } |
118 | |
125 | |
119 | void html_hidden(char *name, char *value) |
126 | void html_hidden(char *name, char *value) |
120 | { |
127 | { |
121 | html("<input type='hidden' name='"); |
128 | html("<input type='hidden' name='"); |
122 | html_attr(name); |
129 | html_attr(name); |
123 | html("' value='"); |
130 | html("' value='"); |
124 | html_attr(value); |
131 | html_attr(value); |
125 | html("'/>"); |
132 | html("'/>"); |
126 | } |
133 | } |
127 | |
134 | |
128 | void html_option(char *value, char *text, char *selected_value) |
135 | void html_option(char *value, char *text, char *selected_value) |
129 | { |
136 | { |
130 | html("<option value='"); |
137 | html("<option value='"); |
131 | html_attr(value); |
138 | html_attr(value); |
132 | html("'"); |
139 | html("'"); |
133 | if (selected_value && !strcmp(selected_value, value)) |
140 | if (selected_value && !strcmp(selected_value, value)) |
134 | html(" selected='selected'"); |
141 | html(" selected='selected'"); |
135 | html(">"); |
142 | html(">"); |
136 | html_txt(text); |
143 | html_txt(text); |
137 | html("</option>\n"); |
144 | html("</option>\n"); |
138 | } |
145 | } |
139 | |
146 | |
140 | void html_link_open(char *url, char *title, char *class) |
147 | void html_link_open(char *url, char *title, char *class) |
141 | { |
148 | { |
142 | html("<a href='"); |
149 | html("<a href='"); |
143 | html_attr(url); |
150 | html_attr(url); |
144 | if (title) { |
151 | if (title) { |
145 | html("' title='"); |
152 | html("' title='"); |
146 | html_attr(title); |
153 | html_attr(title); |
147 | } |
154 | } |
148 | if (class) { |
155 | if (class) { |
149 | html("' class='"); |
156 | html("' class='"); |
150 | html_attr(class); |
157 | html_attr(class); |
151 | } |
158 | } |
152 | html("'>"); |
159 | html("'>"); |
153 | } |
160 | } |
154 | |
161 | |
155 | void html_link_close(void) |
162 | void html_link_close(void) |
156 | { |
163 | { |
157 | html("</a>"); |
164 | html("</a>"); |
158 | } |
165 | } |
159 | |
166 | |
160 | void html_fileperm(unsigned short mode) |
167 | void html_fileperm(unsigned short mode) |
161 | { |
168 | { |
162 | htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), |
169 | htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), |
163 | (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); |
170 | (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-')); |
164 | } |
171 | } |
165 | |
172 | |
166 | int html_include(const char *filename) |
173 | int html_include(const char *filename) |
167 | { |
174 | { |
168 | FILE *f; |
175 | FILE *f; |
169 | char buf[4096]; |
176 | char buf[4096]; |
170 | size_t len; |
177 | size_t len; |
171 | |
178 | |
172 | if (!(f = fopen(filename, "r"))) { |
179 | if (!(f = fopen(filename, "r"))) { |
173 | fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n", |
180 | fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n", |
174 | filename, strerror(errno), errno); |
181 | filename, strerror(errno), errno); |
175 | return -1; |
182 | return -1; |
176 | } |
183 | } |
177 | while((len = fread(buf, 1, 4096, f)) > 0) |
184 | while((len = fread(buf, 1, 4096, f)) > 0) |
178 | write(htmlfd, buf, len); |
185 | write(htmlfd, buf, len); |
179 | fclose(f); |
186 | fclose(f); |
180 | return 0; |
187 | return 0; |
181 | } |
188 | } |
182 | |
189 | |
183 | int hextoint(char c) |
190 | int hextoint(char c) |
184 | { |
191 | { |
185 | if (c >= 'a' && c <= 'f') |
192 | if (c >= 'a' && c <= 'f') |
186 | return 10 + c - 'a'; |
193 | return 10 + c - 'a'; |
187 | else if (c >= 'A' && c <= 'F') |
194 | else if (c >= 'A' && c <= 'F') |
188 | return 10 + c - 'A'; |
195 | return 10 + c - 'A'; |
189 | else if (c >= '0' && c <= '9') |
196 | else if (c >= '0' && c <= '9') |
190 | return c - '0'; |
197 | return c - '0'; |
191 | else |
198 | else |
192 | return -1; |
199 | return -1; |
193 | } |
200 | } |
194 | |
201 | |
195 | char *convert_query_hexchar(char *txt) |
202 | char *convert_query_hexchar(char *txt) |
196 | { |
203 | { |
197 | int d1, d2; |
204 | int d1, d2; |
198 | if (strlen(txt) < 3) { |
205 | if (strlen(txt) < 3) { |
199 | *txt = '\0'; |
206 | *txt = '\0'; |
200 | return txt-1; |
207 | return txt-1; |
201 | } |
208 | } |
202 | d1 = hextoint(*(txt+1)); |
209 | d1 = hextoint(*(txt+1)); |
203 | d2 = hextoint(*(txt+2)); |
210 | d2 = hextoint(*(txt+2)); |
204 | if (d1<0 || d2<0) { |
211 | if (d1<0 || d2<0) { |
205 | strcpy(txt, txt+3); |
212 | strcpy(txt, txt+3); |
206 | return txt-1; |
213 | return txt-1; |
207 | } else { |
214 | } else { |
208 | *txt = d1 * 16 + d2; |
215 | *txt = d1 * 16 + d2; |
209 | strcpy(txt+1, txt+3); |
216 | strcpy(txt+1, txt+3); |
210 | return txt; |
217 | return txt; |
211 | } |
218 | } |
212 | } |
219 | } |
213 | |
220 | |
214 | int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)) |
221 | int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value)) |
215 | { |
222 | { |
216 | char *t, *value = NULL, c; |
223 | char *t, *value = NULL, c; |
217 | |
224 | |
218 | if (!txt) |
225 | if (!txt) |
219 | return 0; |
226 | return 0; |
220 | |
227 | |
221 | t = txt = strdup(txt); |
228 | t = txt = strdup(txt); |
222 | if (t == NULL) { |
229 | if (t == NULL) { |
223 | printf("Out of memory\n"); |
230 | printf("Out of memory\n"); |
224 | exit(1); |
231 | exit(1); |
225 | } |
232 | } |
226 | while((c=*t) != '\0') { |
233 | while((c=*t) != '\0') { |
227 | if (c=='=') { |
234 | if (c=='=') { |
228 | *t = '\0'; |
235 | *t = '\0'; |
229 | value = t+1; |
236 | value = t+1; |
230 | } else if (c=='+') { |
237 | } else if (c=='+') { |
231 | *t = ' '; |
238 | *t = ' '; |
232 | } else if (c=='%') { |
239 | } else if (c=='%') { |
233 | t = convert_query_hexchar(t); |
240 | t = convert_query_hexchar(t); |
234 | } else if (c=='&') { |
241 | } else if (c=='&') { |
235 | *t = '\0'; |
242 | *t = '\0'; |
236 | (*fn)(txt, value); |
243 | (*fn)(txt, value); |
237 | txt = t+1; |
244 | txt = t+1; |
238 | value = NULL; |
245 | value = NULL; |
239 | } |
246 | } |
240 | t++; |
247 | t++; |
241 | } |
248 | } |
242 | if (t!=txt) |
249 | if (t!=txt) |
243 | (*fn)(txt, value); |
250 | (*fn)(txt, value); |
244 | return 0; |
251 | return 0; |
245 | } |
252 | } |
|