author | Lars Hjemli <hjemli@gmail.com> | 2008-04-08 19:29:21 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-04-08 19:29:21 (UTC) |
commit | 23296ad648c0e2a9e3cf40a3de322b10ad25cce3 (patch) (unidiff) | |
tree | 136493d8228b0ff4971feb06b0e8aee296367b00 /html.c | |
parent | e2a44cf0923398396b7a321d5ce894ad3bf6f580 (diff) | |
parent | c6f747649ace1a92ed5dfaae9cc1ea3affe0bf51 (diff) | |
download | cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.zip cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.tar.gz cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.tar.bz2 |
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
...
-rw-r--r-- | html.c | 95 |
1 files changed, 76 insertions, 19 deletions
@@ -1,184 +1,241 @@ | |||
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); |
42 | html(buf); | 50 | html(buf); |
43 | } | 51 | } |
44 | 52 | ||
45 | void html_txt(char *txt) | 53 | void html_txt(char *txt) |
46 | { | 54 | { |
47 | char *t = txt; | 55 | char *t = txt; |
48 | while(t && *t){ | 56 | while(t && *t){ |
49 | int c = *t; | 57 | int c = *t; |
50 | if (c=='<' || c=='>' || c=='&') { | 58 | if (c=='<' || c=='>' || c=='&') { |
51 | write(htmlfd, txt, t - txt); | 59 | write(htmlfd, txt, t - txt); |
52 | if (c=='>') | 60 | if (c=='>') |
53 | html(">"); | 61 | html(">"); |
54 | else if (c=='<') | 62 | else if (c=='<') |
55 | html("<"); | 63 | html("<"); |
56 | else if (c=='&') | 64 | else if (c=='&') |
57 | html("&"); | 65 | html("&"); |
58 | txt = t+1; | 66 | txt = t+1; |
59 | } | 67 | } |
60 | t++; | 68 | t++; |
61 | } | 69 | } |
62 | if (t!=txt) | 70 | if (t!=txt) |
63 | html(txt); | 71 | html(txt); |
64 | } | 72 | } |
65 | 73 | ||
66 | void html_ntxt(int len, char *txt) | 74 | void html_ntxt(int len, char *txt) |
67 | { | 75 | { |
68 | char *t = txt; | 76 | char *t = txt; |
69 | while(t && *t && len--){ | 77 | while(t && *t && len--){ |
70 | int c = *t; | 78 | int c = *t; |
71 | if (c=='<' || c=='>' || c=='&') { | 79 | if (c=='<' || c=='>' || c=='&') { |
72 | write(htmlfd, txt, t - txt); | 80 | write(htmlfd, txt, t - txt); |
73 | if (c=='>') | 81 | if (c=='>') |
74 | html(">"); | 82 | html(">"); |
75 | else if (c=='<') | 83 | else if (c=='<') |
76 | html("<"); | 84 | html("<"); |
77 | else if (c=='&') | 85 | else if (c=='&') |
78 | html("&"); | 86 | html("&"); |
79 | txt = t+1; | 87 | txt = t+1; |
80 | } | 88 | } |
81 | t++; | 89 | t++; |
82 | } | 90 | } |
83 | if (t!=txt) | 91 | if (t!=txt) |
84 | write(htmlfd, txt, t - txt); | 92 | write(htmlfd, txt, t - txt); |
85 | if (len<0) | 93 | if (len<0) |
86 | html("..."); | 94 | html("..."); |
87 | } | 95 | } |
88 | 96 | ||
89 | void html_attr(char *txt) | 97 | void html_attr(char *txt) |
90 | { | 98 | { |
91 | char *t = txt; | 99 | char *t = txt; |
92 | while(t && *t){ | 100 | while(t && *t){ |
93 | int c = *t; | 101 | int c = *t; |
94 | if (c=='<' || c=='>' || c=='\'') { | 102 | if (c=='<' || c=='>' || c=='\'') { |
95 | write(htmlfd, txt, t - txt); | 103 | write(htmlfd, txt, t - txt); |
96 | if (c=='>') | 104 | if (c=='>') |
97 | html(">"); | 105 | html(">"); |
98 | else if (c=='<') | 106 | else if (c=='<') |
99 | html("<"); | 107 | html("<"); |
100 | else if (c=='\'') | 108 | else if (c=='\'') |
101 | html(""e;"); | 109 | html(""e;"); |
102 | txt = t+1; | 110 | txt = t+1; |
103 | } | 111 | } |
104 | t++; | 112 | t++; |
105 | } | 113 | } |
106 | if (t!=txt) | 114 | if (t!=txt) |
107 | html(txt); | 115 | html(txt); |
108 | } | 116 | } |
109 | 117 | ||
110 | void html_hidden(char *name, char *value) | 118 | void html_hidden(char *name, char *value) |
111 | { | 119 | { |
112 | html("<input type='hidden' name='"); | 120 | html("<input type='hidden' name='"); |
113 | html_attr(name); | 121 | html_attr(name); |
114 | html("' value='"); | 122 | html("' value='"); |
115 | html_attr(value); | 123 | html_attr(value); |
116 | html("'/>"); | 124 | html("'/>"); |
117 | } | 125 | } |
118 | 126 | ||
119 | void html_option(char *value, char *text, char *selected_value) | 127 | void html_option(char *value, char *text, char *selected_value) |
120 | { | 128 | { |
121 | html("<option value='"); | 129 | html("<option value='"); |
122 | html_attr(value); | 130 | html_attr(value); |
123 | html("'"); | 131 | html("'"); |
124 | if (selected_value && !strcmp(selected_value, value)) | 132 | if (selected_value && !strcmp(selected_value, value)) |
125 | html(" selected='selected'"); | 133 | html(" selected='selected'"); |
126 | html(">"); | 134 | html(">"); |
127 | html_txt(text); | 135 | html_txt(text); |
128 | html("</option>\n"); | 136 | html("</option>\n"); |
129 | } | 137 | } |
130 | 138 | ||
131 | void html_link_open(char *url, char *title, char *class) | 139 | void html_link_open(char *url, char *title, char *class) |
132 | { | 140 | { |
133 | html("<a href='"); | 141 | html("<a href='"); |
134 | html_attr(url); | 142 | html_attr(url); |
135 | if (title) { | 143 | if (title) { |
136 | html("' title='"); | 144 | html("' title='"); |
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 | } | ||