author | Jonathan Bastien-Filiatrault <joe@x2a.org> | 2007-10-26 22:11:26 (UTC) |
---|---|---|
committer | Jonathan Bastien-Filiatrault <joe@x2a.org> | 2007-11-05 23:13:30 (UTC) |
commit | af0819830445e39584a0137034562086a55deaf2 (patch) (unidiff) | |
tree | a9da08806d706be633b63d0fc2f9dbe12824536b | |
parent | aa5cc328f4894ba6972842b4edbca3017f169050 (diff) | |
download | cgit-af0819830445e39584a0137034562086a55deaf2.zip cgit-af0819830445e39584a0137034562086a55deaf2.tar.gz cgit-af0819830445e39584a0137034562086a55deaf2.tar.bz2 |
Add iconv_msg function.
-rw-r--r-- | parsing.c | 58 |
1 files changed, 58 insertions, 0 deletions
@@ -1,316 +1,374 @@ | |||
1 | /* config.c: parsing of config files | 1 | /* config.c: parsing of config files |
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 <iconv.h> | ||
10 | |||
9 | #include "cgit.h" | 11 | #include "cgit.h" |
10 | 12 | ||
11 | int next_char(FILE *f) | 13 | int next_char(FILE *f) |
12 | { | 14 | { |
13 | int c = fgetc(f); | 15 | int c = fgetc(f); |
14 | if (c=='\r') { | 16 | if (c=='\r') { |
15 | c = fgetc(f); | 17 | c = fgetc(f); |
16 | if (c!='\n') { | 18 | if (c!='\n') { |
17 | ungetc(c, f); | 19 | ungetc(c, f); |
18 | c = '\r'; | 20 | c = '\r'; |
19 | } | 21 | } |
20 | } | 22 | } |
21 | return c; | 23 | return c; |
22 | } | 24 | } |
23 | 25 | ||
24 | void skip_line(FILE *f) | 26 | void skip_line(FILE *f) |
25 | { | 27 | { |
26 | int c; | 28 | int c; |
27 | 29 | ||
28 | while((c=next_char(f)) && c!='\n' && c!=EOF) | 30 | while((c=next_char(f)) && c!='\n' && c!=EOF) |
29 | ; | 31 | ; |
30 | } | 32 | } |
31 | 33 | ||
32 | int read_config_line(FILE *f, char *line, const char **value, int bufsize) | 34 | int read_config_line(FILE *f, char *line, const char **value, int bufsize) |
33 | { | 35 | { |
34 | int i = 0, isname = 0; | 36 | int i = 0, isname = 0; |
35 | 37 | ||
36 | *value = NULL; | 38 | *value = NULL; |
37 | while(i<bufsize-1) { | 39 | while(i<bufsize-1) { |
38 | int c = next_char(f); | 40 | int c = next_char(f); |
39 | if (!isname && (c=='#' || c==';')) { | 41 | if (!isname && (c=='#' || c==';')) { |
40 | skip_line(f); | 42 | skip_line(f); |
41 | continue; | 43 | continue; |
42 | } | 44 | } |
43 | if (!isname && isspace(c)) | 45 | if (!isname && isspace(c)) |
44 | continue; | 46 | continue; |
45 | 47 | ||
46 | if (c=='=' && !*value) { | 48 | if (c=='=' && !*value) { |
47 | line[i] = 0; | 49 | line[i] = 0; |
48 | *value = &line[i+1]; | 50 | *value = &line[i+1]; |
49 | } else if (c=='\n' && !isname) { | 51 | } else if (c=='\n' && !isname) { |
50 | i = 0; | 52 | i = 0; |
51 | continue; | 53 | continue; |
52 | } else if (c=='\n' || c==EOF) { | 54 | } else if (c=='\n' || c==EOF) { |
53 | line[i] = 0; | 55 | line[i] = 0; |
54 | break; | 56 | break; |
55 | } else { | 57 | } else { |
56 | line[i]=c; | 58 | line[i]=c; |
57 | } | 59 | } |
58 | isname = 1; | 60 | isname = 1; |
59 | i++; | 61 | i++; |
60 | } | 62 | } |
61 | line[i+1] = 0; | 63 | line[i+1] = 0; |
62 | return i; | 64 | return i; |
63 | } | 65 | } |
64 | 66 | ||
65 | int cgit_read_config(const char *filename, configfn fn) | 67 | int cgit_read_config(const char *filename, configfn fn) |
66 | { | 68 | { |
67 | static int nesting; | 69 | static int nesting; |
68 | int len; | 70 | int len; |
69 | char line[256]; | 71 | char line[256]; |
70 | const char *value; | 72 | const char *value; |
71 | FILE *f; | 73 | FILE *f; |
72 | 74 | ||
73 | /* cancel deeply nested include-commands */ | 75 | /* cancel deeply nested include-commands */ |
74 | if (nesting > 8) | 76 | if (nesting > 8) |
75 | return -1; | 77 | return -1; |
76 | if (!(f = fopen(filename, "r"))) | 78 | if (!(f = fopen(filename, "r"))) |
77 | return -1; | 79 | return -1; |
78 | nesting++; | 80 | nesting++; |
79 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) | 81 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) |
80 | (*fn)(line, value); | 82 | (*fn)(line, value); |
81 | nesting--; | 83 | nesting--; |
82 | fclose(f); | 84 | fclose(f); |
83 | return 0; | 85 | return 0; |
84 | } | 86 | } |
85 | 87 | ||
86 | char *convert_query_hexchar(char *txt) | 88 | char *convert_query_hexchar(char *txt) |
87 | { | 89 | { |
88 | int d1, d2; | 90 | int d1, d2; |
89 | if (strlen(txt) < 3) { | 91 | if (strlen(txt) < 3) { |
90 | *txt = '\0'; | 92 | *txt = '\0'; |
91 | return txt-1; | 93 | return txt-1; |
92 | } | 94 | } |
93 | d1 = hextoint(*(txt+1)); | 95 | d1 = hextoint(*(txt+1)); |
94 | d2 = hextoint(*(txt+2)); | 96 | d2 = hextoint(*(txt+2)); |
95 | if (d1<0 || d2<0) { | 97 | if (d1<0 || d2<0) { |
96 | strcpy(txt, txt+3); | 98 | strcpy(txt, txt+3); |
97 | return txt-1; | 99 | return txt-1; |
98 | } else { | 100 | } else { |
99 | *txt = d1 * 16 + d2; | 101 | *txt = d1 * 16 + d2; |
100 | strcpy(txt+1, txt+3); | 102 | strcpy(txt+1, txt+3); |
101 | return txt; | 103 | return txt; |
102 | } | 104 | } |
103 | } | 105 | } |
104 | 106 | ||
105 | int cgit_parse_query(char *txt, configfn fn) | 107 | int cgit_parse_query(char *txt, configfn fn) |
106 | { | 108 | { |
107 | char *t, *value = NULL, c; | 109 | char *t, *value = NULL, c; |
108 | 110 | ||
109 | if (!txt) | 111 | if (!txt) |
110 | return 0; | 112 | return 0; |
111 | 113 | ||
112 | t = txt = xstrdup(txt); | 114 | t = txt = xstrdup(txt); |
113 | 115 | ||
114 | while((c=*t) != '\0') { | 116 | while((c=*t) != '\0') { |
115 | if (c=='=') { | 117 | if (c=='=') { |
116 | *t = '\0'; | 118 | *t = '\0'; |
117 | value = t+1; | 119 | value = t+1; |
118 | } else if (c=='+') { | 120 | } else if (c=='+') { |
119 | *t = ' '; | 121 | *t = ' '; |
120 | } else if (c=='%') { | 122 | } else if (c=='%') { |
121 | t = convert_query_hexchar(t); | 123 | t = convert_query_hexchar(t); |
122 | } else if (c=='&') { | 124 | } else if (c=='&') { |
123 | *t = '\0'; | 125 | *t = '\0'; |
124 | (*fn)(txt, value); | 126 | (*fn)(txt, value); |
125 | txt = t+1; | 127 | txt = t+1; |
126 | value = NULL; | 128 | value = NULL; |
127 | } | 129 | } |
128 | t++; | 130 | t++; |
129 | } | 131 | } |
130 | if (t!=txt) | 132 | if (t!=txt) |
131 | (*fn)(txt, value); | 133 | (*fn)(txt, value); |
132 | return 0; | 134 | return 0; |
133 | } | 135 | } |
134 | 136 | ||
135 | /* | 137 | /* |
136 | * url syntax: [repo ['/' cmd [ '/' path]]] | 138 | * url syntax: [repo ['/' cmd [ '/' path]]] |
137 | * repo: any valid repo url, may contain '/' | 139 | * repo: any valid repo url, may contain '/' |
138 | * cmd: log | commit | diff | tree | view | blob | snapshot | 140 | * cmd: log | commit | diff | tree | view | blob | snapshot |
139 | * path: any valid path, may contain '/' | 141 | * path: any valid path, may contain '/' |
140 | * | 142 | * |
141 | */ | 143 | */ |
142 | void cgit_parse_url(const char *url) | 144 | void cgit_parse_url(const char *url) |
143 | { | 145 | { |
144 | char *cmd, *p; | 146 | char *cmd, *p; |
145 | 147 | ||
146 | cgit_repo = NULL; | 148 | cgit_repo = NULL; |
147 | if (!url || url[0] == '\0') | 149 | if (!url || url[0] == '\0') |
148 | return; | 150 | return; |
149 | 151 | ||
150 | cgit_repo = cgit_get_repoinfo(url); | 152 | cgit_repo = cgit_get_repoinfo(url); |
151 | if (cgit_repo) { | 153 | if (cgit_repo) { |
152 | cgit_query_repo = cgit_repo->url; | 154 | cgit_query_repo = cgit_repo->url; |
153 | return; | 155 | return; |
154 | } | 156 | } |
155 | 157 | ||
156 | cmd = strchr(url, '/'); | 158 | cmd = strchr(url, '/'); |
157 | while (!cgit_repo && cmd) { | 159 | while (!cgit_repo && cmd) { |
158 | cmd[0] = '\0'; | 160 | cmd[0] = '\0'; |
159 | cgit_repo = cgit_get_repoinfo(url); | 161 | cgit_repo = cgit_get_repoinfo(url); |
160 | if (cgit_repo == NULL) { | 162 | if (cgit_repo == NULL) { |
161 | cmd[0] = '/'; | 163 | cmd[0] = '/'; |
162 | cmd = strchr(cmd + 1, '/'); | 164 | cmd = strchr(cmd + 1, '/'); |
163 | continue; | 165 | continue; |
164 | } | 166 | } |
165 | 167 | ||
166 | cgit_query_repo = cgit_repo->url; | 168 | cgit_query_repo = cgit_repo->url; |
167 | p = strchr(cmd + 1, '/'); | 169 | p = strchr(cmd + 1, '/'); |
168 | if (p) { | 170 | if (p) { |
169 | p[0] = '\0'; | 171 | p[0] = '\0'; |
170 | if (p[1]) | 172 | if (p[1]) |
171 | cgit_query_path = trim_end(p + 1, '/'); | 173 | cgit_query_path = trim_end(p + 1, '/'); |
172 | } | 174 | } |
173 | cgit_cmd = cgit_get_cmd_index(cmd + 1); | 175 | cgit_cmd = cgit_get_cmd_index(cmd + 1); |
174 | cgit_query_page = xstrdup(cmd + 1); | 176 | cgit_query_page = xstrdup(cmd + 1); |
175 | return; | 177 | return; |
176 | } | 178 | } |
177 | } | 179 | } |
178 | 180 | ||
181 | static char *iconv_msg(char *msg, const char *encoding) | ||
182 | { | ||
183 | iconv_t msg_conv = iconv_open(PAGE_ENCODING, encoding); | ||
184 | size_t inlen = strlen(msg); | ||
185 | char *in; | ||
186 | char *out; | ||
187 | size_t inleft; | ||
188 | size_t outleft; | ||
189 | char *buf; | ||
190 | char *ret; | ||
191 | size_t buf_sz; | ||
192 | int again, fail; | ||
193 | |||
194 | if(msg_conv == (iconv_t)-1) | ||
195 | return NULL; | ||
196 | |||
197 | buf_sz = inlen * 2; | ||
198 | buf = xmalloc(buf_sz+1); | ||
199 | do { | ||
200 | in = msg; | ||
201 | inleft = inlen; | ||
202 | |||
203 | out = buf; | ||
204 | outleft = buf_sz; | ||
205 | iconv(msg_conv, &in, &inleft, &out, &outleft); | ||
206 | |||
207 | if(inleft == 0) { | ||
208 | fail = 0; | ||
209 | again = 0; | ||
210 | } else if(inleft != 0 && errno == E2BIG) { | ||
211 | fail = 0; | ||
212 | again = 1; | ||
213 | |||
214 | buf_sz *= 2; | ||
215 | free(buf); | ||
216 | buf = xmalloc(buf_sz+1); | ||
217 | } else { | ||
218 | fail = 1; | ||
219 | again = 0; | ||
220 | } | ||
221 | } while(again && !fail); | ||
222 | |||
223 | if(fail) { | ||
224 | free(buf); | ||
225 | ret = NULL; | ||
226 | } else { | ||
227 | buf = xrealloc(buf, out - buf); | ||
228 | *out = 0; | ||
229 | ret = buf; | ||
230 | } | ||
231 | |||
232 | iconv_close(msg_conv); | ||
233 | |||
234 | return ret; | ||
235 | } | ||
236 | |||
179 | char *substr(const char *head, const char *tail) | 237 | char *substr(const char *head, const char *tail) |
180 | { | 238 | { |
181 | char *buf; | 239 | char *buf; |
182 | 240 | ||
183 | buf = xmalloc(tail - head + 1); | 241 | buf = xmalloc(tail - head + 1); |
184 | strncpy(buf, head, tail - head); | 242 | strncpy(buf, head, tail - head); |
185 | buf[tail - head] = '\0'; | 243 | buf[tail - head] = '\0'; |
186 | return buf; | 244 | return buf; |
187 | } | 245 | } |
188 | 246 | ||
189 | struct commitinfo *cgit_parse_commit(struct commit *commit) | 247 | struct commitinfo *cgit_parse_commit(struct commit *commit) |
190 | { | 248 | { |
191 | struct commitinfo *ret; | 249 | struct commitinfo *ret; |
192 | char *p = commit->buffer, *t = commit->buffer; | 250 | char *p = commit->buffer, *t = commit->buffer; |
193 | 251 | ||
194 | ret = xmalloc(sizeof(*ret)); | 252 | ret = xmalloc(sizeof(*ret)); |
195 | ret->commit = commit; | 253 | ret->commit = commit; |
196 | ret->author = NULL; | 254 | ret->author = NULL; |
197 | ret->author_email = NULL; | 255 | ret->author_email = NULL; |
198 | ret->committer = NULL; | 256 | ret->committer = NULL; |
199 | ret->committer_email = NULL; | 257 | ret->committer_email = NULL; |
200 | ret->subject = NULL; | 258 | ret->subject = NULL; |
201 | ret->msg = NULL; | 259 | ret->msg = NULL; |
202 | ret->msg_encoding = NULL; | 260 | ret->msg_encoding = NULL; |
203 | 261 | ||
204 | if (p == NULL) | 262 | if (p == NULL) |
205 | return ret; | 263 | return ret; |
206 | 264 | ||
207 | if (strncmp(p, "tree ", 5)) | 265 | if (strncmp(p, "tree ", 5)) |
208 | die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); | 266 | die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); |
209 | else | 267 | else |
210 | p += 46; // "tree " + hex[40] + "\n" | 268 | p += 46; // "tree " + hex[40] + "\n" |
211 | 269 | ||
212 | while (!strncmp(p, "parent ", 7)) | 270 | while (!strncmp(p, "parent ", 7)) |
213 | p += 48; // "parent " + hex[40] + "\n" | 271 | p += 48; // "parent " + hex[40] + "\n" |
214 | 272 | ||
215 | if (!strncmp(p, "author ", 7)) { | 273 | if (!strncmp(p, "author ", 7)) { |
216 | p += 7; | 274 | p += 7; |
217 | t = strchr(p, '<') - 1; | 275 | t = strchr(p, '<') - 1; |
218 | ret->author = substr(p, t); | 276 | ret->author = substr(p, t); |
219 | p = t; | 277 | p = t; |
220 | t = strchr(t, '>') + 1; | 278 | t = strchr(t, '>') + 1; |
221 | ret->author_email = substr(p, t); | 279 | ret->author_email = substr(p, t); |
222 | ret->author_date = atol(++t); | 280 | ret->author_date = atol(++t); |
223 | p = strchr(t, '\n') + 1; | 281 | p = strchr(t, '\n') + 1; |
224 | } | 282 | } |
225 | 283 | ||
226 | if (!strncmp(p, "committer ", 9)) { | 284 | if (!strncmp(p, "committer ", 9)) { |
227 | p += 9; | 285 | p += 9; |
228 | t = strchr(p, '<') - 1; | 286 | t = strchr(p, '<') - 1; |
229 | ret->committer = substr(p, t); | 287 | ret->committer = substr(p, t); |
230 | p = t; | 288 | p = t; |
231 | t = strchr(t, '>') + 1; | 289 | t = strchr(t, '>') + 1; |
232 | ret->committer_email = substr(p, t); | 290 | ret->committer_email = substr(p, t); |
233 | ret->committer_date = atol(++t); | 291 | ret->committer_date = atol(++t); |
234 | p = strchr(t, '\n') + 1; | 292 | p = strchr(t, '\n') + 1; |
235 | } | 293 | } |
236 | 294 | ||
237 | if (!strncmp(p, "encoding ", 9)) { | 295 | if (!strncmp(p, "encoding ", 9)) { |
238 | p += 9; | 296 | p += 9; |
239 | t = strchr(p, '\n') + 1; | 297 | t = strchr(p, '\n') + 1; |
240 | ret->msg_encoding = substr(p, t); | 298 | ret->msg_encoding = substr(p, t); |
241 | p = t; | 299 | p = t; |
242 | } else | 300 | } else |
243 | ret->msg_encoding = xstrdup(PAGE_ENCODING); | 301 | ret->msg_encoding = xstrdup(PAGE_ENCODING); |
244 | 302 | ||
245 | while (*p && (*p != '\n')) | 303 | while (*p && (*p != '\n')) |
246 | p = strchr(p, '\n') + 1; // skip unknown header fields | 304 | p = strchr(p, '\n') + 1; // skip unknown header fields |
247 | 305 | ||
248 | while (*p == '\n') | 306 | while (*p == '\n') |
249 | p = strchr(p, '\n') + 1; | 307 | p = strchr(p, '\n') + 1; |
250 | 308 | ||
251 | t = strchr(p, '\n'); | 309 | t = strchr(p, '\n'); |
252 | if (t) { | 310 | if (t) { |
253 | if (*t == '\0') | 311 | if (*t == '\0') |
254 | ret->subject = "** empty **"; | 312 | ret->subject = "** empty **"; |
255 | else | 313 | else |
256 | ret->subject = substr(p, t); | 314 | ret->subject = substr(p, t); |
257 | p = t + 1; | 315 | p = t + 1; |
258 | 316 | ||
259 | while (*p == '\n') | 317 | while (*p == '\n') |
260 | p = strchr(p, '\n') + 1; | 318 | p = strchr(p, '\n') + 1; |
261 | ret->msg = xstrdup(p); | 319 | ret->msg = xstrdup(p); |
262 | } else | 320 | } else |
263 | ret->subject = substr(p, p+strlen(p)); | 321 | ret->subject = substr(p, p+strlen(p)); |
264 | 322 | ||
265 | return ret; | 323 | return ret; |
266 | } | 324 | } |
267 | 325 | ||
268 | 326 | ||
269 | struct taginfo *cgit_parse_tag(struct tag *tag) | 327 | struct taginfo *cgit_parse_tag(struct tag *tag) |
270 | { | 328 | { |
271 | void *data; | 329 | void *data; |
272 | enum object_type type; | 330 | enum object_type type; |
273 | unsigned long size; | 331 | unsigned long size; |
274 | char *p, *t; | 332 | char *p, *t; |
275 | struct taginfo *ret; | 333 | struct taginfo *ret; |
276 | 334 | ||
277 | data = read_sha1_file(tag->object.sha1, &type, &size); | 335 | data = read_sha1_file(tag->object.sha1, &type, &size); |
278 | if (!data || type != OBJ_TAG) { | 336 | if (!data || type != OBJ_TAG) { |
279 | free(data); | 337 | free(data); |
280 | return 0; | 338 | return 0; |
281 | } | 339 | } |
282 | 340 | ||
283 | ret = xmalloc(sizeof(*ret)); | 341 | ret = xmalloc(sizeof(*ret)); |
284 | ret->tagger = NULL; | 342 | ret->tagger = NULL; |
285 | ret->tagger_email = NULL; | 343 | ret->tagger_email = NULL; |
286 | ret->tagger_date = 0; | 344 | ret->tagger_date = 0; |
287 | ret->msg = NULL; | 345 | ret->msg = NULL; |
288 | 346 | ||
289 | p = data; | 347 | p = data; |
290 | 348 | ||
291 | while (p && *p) { | 349 | while (p && *p) { |
292 | if (*p == '\n') | 350 | if (*p == '\n') |
293 | break; | 351 | break; |
294 | 352 | ||
295 | if (!strncmp(p, "tagger ", 7)) { | 353 | if (!strncmp(p, "tagger ", 7)) { |
296 | p += 7; | 354 | p += 7; |
297 | t = strchr(p, '<') - 1; | 355 | t = strchr(p, '<') - 1; |
298 | ret->tagger = substr(p, t); | 356 | ret->tagger = substr(p, t); |
299 | p = t; | 357 | p = t; |
300 | t = strchr(t, '>') + 1; | 358 | t = strchr(t, '>') + 1; |
301 | ret->tagger_email = substr(p, t); | 359 | ret->tagger_email = substr(p, t); |
302 | ret->tagger_date = atol(++t); | 360 | ret->tagger_date = atol(++t); |
303 | } | 361 | } |
304 | p = strchr(p, '\n') + 1; | 362 | p = strchr(p, '\n') + 1; |
305 | } | 363 | } |
306 | 364 | ||
307 | while (p && *p && (*p != '\n')) | 365 | while (p && *p && (*p != '\n')) |
308 | p = strchr(p, '\n') + 1; // skip unknown tag fields | 366 | p = strchr(p, '\n') + 1; // skip unknown tag fields |
309 | 367 | ||
310 | while (p && (*p == '\n')) | 368 | while (p && (*p == '\n')) |
311 | p = strchr(p, '\n') + 1; | 369 | p = strchr(p, '\n') + 1; |
312 | if (p && *p) | 370 | if (p && *p) |
313 | ret->msg = xstrdup(p); | 371 | ret->msg = xstrdup(p); |
314 | free(data); | 372 | free(data); |
315 | return ret; | 373 | return ret; |
316 | } | 374 | } |