summaryrefslogtreecommitdiffabout
path: root/parsing.c
Unidiff
Diffstat (limited to 'parsing.c') (more/less context) (ignore whitespace changes)
-rw-r--r--parsing.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/parsing.c b/parsing.c
index f156c12..c731084 100644
--- a/parsing.c
+++ b/parsing.c
@@ -131,244 +131,258 @@ int cgit_parse_query(char *txt, configfn fn)
131 } 131 }
132 if (t!=txt) 132 if (t!=txt)
133 (*fn)(txt, value); 133 (*fn)(txt, value);
134 return 0; 134 return 0;
135} 135}
136 136
137/* 137/*
138 * url syntax: [repo ['/' cmd [ '/' path]]] 138 * url syntax: [repo ['/' cmd [ '/' path]]]
139 * repo: any valid repo url, may contain '/' 139 * repo: any valid repo url, may contain '/'
140 * cmd: log | commit | diff | tree | view | blob | snapshot 140 * cmd: log | commit | diff | tree | view | blob | snapshot
141 * path: any valid path, may contain '/' 141 * path: any valid path, may contain '/'
142 * 142 *
143 */ 143 */
144void cgit_parse_url(const char *url) 144void cgit_parse_url(const char *url)
145{ 145{
146 char *cmd, *p; 146 char *cmd, *p;
147 147
148 cgit_repo = NULL; 148 cgit_repo = NULL;
149 if (!url || url[0] == '\0') 149 if (!url || url[0] == '\0')
150 return; 150 return;
151 151
152 cgit_repo = cgit_get_repoinfo(url); 152 cgit_repo = cgit_get_repoinfo(url);
153 if (cgit_repo) { 153 if (cgit_repo) {
154 cgit_query_repo = cgit_repo->url; 154 cgit_query_repo = cgit_repo->url;
155 return; 155 return;
156 } 156 }
157 157
158 cmd = strchr(url, '/'); 158 cmd = strchr(url, '/');
159 while (!cgit_repo && cmd) { 159 while (!cgit_repo && cmd) {
160 cmd[0] = '\0'; 160 cmd[0] = '\0';
161 cgit_repo = cgit_get_repoinfo(url); 161 cgit_repo = cgit_get_repoinfo(url);
162 if (cgit_repo == NULL) { 162 if (cgit_repo == NULL) {
163 cmd[0] = '/'; 163 cmd[0] = '/';
164 cmd = strchr(cmd + 1, '/'); 164 cmd = strchr(cmd + 1, '/');
165 continue; 165 continue;
166 } 166 }
167 167
168 cgit_query_repo = cgit_repo->url; 168 cgit_query_repo = cgit_repo->url;
169 p = strchr(cmd + 1, '/'); 169 p = strchr(cmd + 1, '/');
170 if (p) { 170 if (p) {
171 p[0] = '\0'; 171 p[0] = '\0';
172 if (p[1]) 172 if (p[1])
173 cgit_query_path = trim_end(p + 1, '/'); 173 cgit_query_path = trim_end(p + 1, '/');
174 } 174 }
175 cgit_cmd = cgit_get_cmd_index(cmd + 1); 175 cgit_cmd = cgit_get_cmd_index(cmd + 1);
176 cgit_query_page = xstrdup(cmd + 1); 176 cgit_query_page = xstrdup(cmd + 1);
177 return; 177 return;
178 } 178 }
179} 179}
180 180
181static char *iconv_msg(char *msg, const char *encoding) 181static char *iconv_msg(char *msg, const char *encoding)
182{ 182{
183 iconv_t msg_conv = iconv_open(PAGE_ENCODING, encoding); 183 iconv_t msg_conv = iconv_open(PAGE_ENCODING, encoding);
184 size_t inlen = strlen(msg); 184 size_t inlen = strlen(msg);
185 char *in; 185 char *in;
186 char *out; 186 char *out;
187 size_t inleft; 187 size_t inleft;
188 size_t outleft; 188 size_t outleft;
189 char *buf; 189 char *buf;
190 char *ret; 190 char *ret;
191 size_t buf_sz; 191 size_t buf_sz;
192 int again, fail; 192 int again, fail;
193 193
194 if(msg_conv == (iconv_t)-1) 194 if(msg_conv == (iconv_t)-1)
195 return NULL; 195 return NULL;
196 196
197 buf_sz = inlen * 2; 197 buf_sz = inlen * 2;
198 buf = xmalloc(buf_sz+1); 198 buf = xmalloc(buf_sz+1);
199 do { 199 do {
200 in = msg; 200 in = msg;
201 inleft = inlen; 201 inleft = inlen;
202 202
203 out = buf; 203 out = buf;
204 outleft = buf_sz; 204 outleft = buf_sz;
205 iconv(msg_conv, &in, &inleft, &out, &outleft); 205 iconv(msg_conv, &in, &inleft, &out, &outleft);
206 206
207 if(inleft == 0) { 207 if(inleft == 0) {
208 fail = 0; 208 fail = 0;
209 again = 0; 209 again = 0;
210 } else if(inleft != 0 && errno == E2BIG) { 210 } else if(inleft != 0 && errno == E2BIG) {
211 fail = 0; 211 fail = 0;
212 again = 1; 212 again = 1;
213 213
214 buf_sz *= 2; 214 buf_sz *= 2;
215 free(buf); 215 free(buf);
216 buf = xmalloc(buf_sz+1); 216 buf = xmalloc(buf_sz+1);
217 } else { 217 } else {
218 fail = 1; 218 fail = 1;
219 again = 0; 219 again = 0;
220 } 220 }
221 } while(again && !fail); 221 } while(again && !fail);
222 222
223 if(fail) { 223 if(fail) {
224 free(buf); 224 free(buf);
225 ret = NULL; 225 ret = NULL;
226 } else { 226 } else {
227 buf = xrealloc(buf, out - buf); 227 buf = xrealloc(buf, out - buf);
228 *out = 0; 228 *out = 0;
229 ret = buf; 229 ret = buf;
230 } 230 }
231 231
232 iconv_close(msg_conv); 232 iconv_close(msg_conv);
233 233
234 return ret; 234 return ret;
235} 235}
236 236
237char *substr(const char *head, const char *tail) 237char *substr(const char *head, const char *tail)
238{ 238{
239 char *buf; 239 char *buf;
240 240
241 buf = xmalloc(tail - head + 1); 241 buf = xmalloc(tail - head + 1);
242 strncpy(buf, head, tail - head); 242 strncpy(buf, head, tail - head);
243 buf[tail - head] = '\0'; 243 buf[tail - head] = '\0';
244 return buf; 244 return buf;
245} 245}
246 246
247struct commitinfo *cgit_parse_commit(struct commit *commit) 247struct commitinfo *cgit_parse_commit(struct commit *commit)
248{ 248{
249 struct commitinfo *ret; 249 struct commitinfo *ret;
250 char *p = commit->buffer, *t = commit->buffer; 250 char *p = commit->buffer, *t = commit->buffer;
251 251
252 ret = xmalloc(sizeof(*ret)); 252 ret = xmalloc(sizeof(*ret));
253 ret->commit = commit; 253 ret->commit = commit;
254 ret->author = NULL; 254 ret->author = NULL;
255 ret->author_email = NULL; 255 ret->author_email = NULL;
256 ret->committer = NULL; 256 ret->committer = NULL;
257 ret->committer_email = NULL; 257 ret->committer_email = NULL;
258 ret->subject = NULL; 258 ret->subject = NULL;
259 ret->msg = NULL; 259 ret->msg = NULL;
260 ret->msg_encoding = NULL; 260 ret->msg_encoding = NULL;
261 261
262 if (p == NULL) 262 if (p == NULL)
263 return ret; 263 return ret;
264 264
265 if (strncmp(p, "tree ", 5)) 265 if (strncmp(p, "tree ", 5))
266 die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); 266 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
267 else 267 else
268 p += 46; // "tree " + hex[40] + "\n" 268 p += 46; // "tree " + hex[40] + "\n"
269 269
270 while (!strncmp(p, "parent ", 7)) 270 while (!strncmp(p, "parent ", 7))
271 p += 48; // "parent " + hex[40] + "\n" 271 p += 48; // "parent " + hex[40] + "\n"
272 272
273 if (!strncmp(p, "author ", 7)) { 273 if (!strncmp(p, "author ", 7)) {
274 p += 7; 274 p += 7;
275 t = strchr(p, '<') - 1; 275 t = strchr(p, '<') - 1;
276 ret->author = substr(p, t); 276 ret->author = substr(p, t);
277 p = t; 277 p = t;
278 t = strchr(t, '>') + 1; 278 t = strchr(t, '>') + 1;
279 ret->author_email = substr(p, t); 279 ret->author_email = substr(p, t);
280 ret->author_date = atol(++t); 280 ret->author_date = atol(++t);
281 p = strchr(t, '\n') + 1; 281 p = strchr(t, '\n') + 1;
282 } 282 }
283 283
284 if (!strncmp(p, "committer ", 9)) { 284 if (!strncmp(p, "committer ", 9)) {
285 p += 9; 285 p += 9;
286 t = strchr(p, '<') - 1; 286 t = strchr(p, '<') - 1;
287 ret->committer = substr(p, t); 287 ret->committer = substr(p, t);
288 p = t; 288 p = t;
289 t = strchr(t, '>') + 1; 289 t = strchr(t, '>') + 1;
290 ret->committer_email = substr(p, t); 290 ret->committer_email = substr(p, t);
291 ret->committer_date = atol(++t); 291 ret->committer_date = atol(++t);
292 p = strchr(t, '\n') + 1; 292 p = strchr(t, '\n') + 1;
293 } 293 }
294 294
295 if (!strncmp(p, "encoding ", 9)) { 295 if (!strncmp(p, "encoding ", 9)) {
296 p += 9; 296 p += 9;
297 t = strchr(p, '\n') + 1; 297 t = strchr(p, '\n') + 1;
298 ret->msg_encoding = substr(p, t); 298 ret->msg_encoding = substr(p, t);
299 p = t; 299 p = t;
300 } else 300 } else
301 ret->msg_encoding = xstrdup(PAGE_ENCODING); 301 ret->msg_encoding = xstrdup(PAGE_ENCODING);
302 302
303 while (*p && (*p != '\n')) 303 while (*p && (*p != '\n'))
304 p = strchr(p, '\n') + 1; // skip unknown header fields 304 p = strchr(p, '\n') + 1; // skip unknown header fields
305 305
306 while (*p == '\n') 306 while (*p == '\n')
307 p = strchr(p, '\n') + 1; 307 p = strchr(p, '\n') + 1;
308 308
309 t = strchr(p, '\n'); 309 t = strchr(p, '\n');
310 if (t) { 310 if (t) {
311 if (*t == '\0') 311 if (*t == '\0')
312 ret->subject = "** empty **"; 312 ret->subject = "** empty **";
313 else 313 else
314 ret->subject = substr(p, t); 314 ret->subject = substr(p, t);
315 p = t + 1; 315 p = t + 1;
316 316
317 while (*p == '\n') 317 while (*p == '\n')
318 p = strchr(p, '\n') + 1; 318 p = strchr(p, '\n') + 1;
319 ret->msg = xstrdup(p); 319 ret->msg = xstrdup(p);
320 } else 320 } else
321 ret->subject = substr(p, p+strlen(p)); 321 ret->subject = substr(p, p+strlen(p));
322 322
323 if(strcmp(ret->msg_encoding, PAGE_ENCODING)) {
324 t = iconv_msg(ret->subject, ret->msg_encoding);
325 if(t) {
326 free(ret->subject);
327 ret->subject = t;
328 }
329
330 t = iconv_msg(ret->msg, ret->msg_encoding);
331 if(t) {
332 free(ret->msg);
333 ret->msg = t;
334 }
335 }
336
323 return ret; 337 return ret;
324} 338}
325 339
326 340
327struct taginfo *cgit_parse_tag(struct tag *tag) 341struct taginfo *cgit_parse_tag(struct tag *tag)
328{ 342{
329 void *data; 343 void *data;
330 enum object_type type; 344 enum object_type type;
331 unsigned long size; 345 unsigned long size;
332 char *p, *t; 346 char *p, *t;
333 struct taginfo *ret; 347 struct taginfo *ret;
334 348
335 data = read_sha1_file(tag->object.sha1, &type, &size); 349 data = read_sha1_file(tag->object.sha1, &type, &size);
336 if (!data || type != OBJ_TAG) { 350 if (!data || type != OBJ_TAG) {
337 free(data); 351 free(data);
338 return 0; 352 return 0;
339 } 353 }
340 354
341 ret = xmalloc(sizeof(*ret)); 355 ret = xmalloc(sizeof(*ret));
342 ret->tagger = NULL; 356 ret->tagger = NULL;
343 ret->tagger_email = NULL; 357 ret->tagger_email = NULL;
344 ret->tagger_date = 0; 358 ret->tagger_date = 0;
345 ret->msg = NULL; 359 ret->msg = NULL;
346 360
347 p = data; 361 p = data;
348 362
349 while (p && *p) { 363 while (p && *p) {
350 if (*p == '\n') 364 if (*p == '\n')
351 break; 365 break;
352 366
353 if (!strncmp(p, "tagger ", 7)) { 367 if (!strncmp(p, "tagger ", 7)) {
354 p += 7; 368 p += 7;
355 t = strchr(p, '<') - 1; 369 t = strchr(p, '<') - 1;
356 ret->tagger = substr(p, t); 370 ret->tagger = substr(p, t);
357 p = t; 371 p = t;
358 t = strchr(t, '>') + 1; 372 t = strchr(t, '>') + 1;
359 ret->tagger_email = substr(p, t); 373 ret->tagger_email = substr(p, t);
360 ret->tagger_date = atol(++t); 374 ret->tagger_date = atol(++t);
361 } 375 }
362 p = strchr(p, '\n') + 1; 376 p = strchr(p, '\n') + 1;
363 } 377 }
364 378
365 while (p && *p && (*p != '\n')) 379 while (p && *p && (*p != '\n'))
366 p = strchr(p, '\n') + 1; // skip unknown tag fields 380 p = strchr(p, '\n') + 1; // skip unknown tag fields
367 381
368 while (p && (*p == '\n')) 382 while (p && (*p == '\n'))
369 p = strchr(p, '\n') + 1; 383 p = strchr(p, '\n') + 1;
370 if (p && *p) 384 if (p && *p)
371 ret->msg = xstrdup(p); 385 ret->msg = xstrdup(p);
372 free(data); 386 free(data);
373 return ret; 387 return ret;
374} 388}