Lars Hjemli | 7640d90 | 2006-12-10 22:41:14 +0100 | [diff] [blame] | 1 | /* config.c: parsing of config files |
| 2 | * |
| 3 | * Copyright (C) 2006 Lars Hjemli |
| 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
Jonathan Bastien-Filiatrault | af08198 | 2007-10-26 18:11:26 -0400 | [diff] [blame^] | 9 | #include <iconv.h> |
| 10 | |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 11 | #include "cgit.h" |
| 12 | |
| 13 | int next_char(FILE *f) |
| 14 | { |
| 15 | int c = fgetc(f); |
| 16 | if (c=='\r') { |
| 17 | c = fgetc(f); |
| 18 | if (c!='\n') { |
| 19 | ungetc(c, f); |
| 20 | c = '\r'; |
| 21 | } |
| 22 | } |
| 23 | return c; |
| 24 | } |
| 25 | |
| 26 | void skip_line(FILE *f) |
| 27 | { |
| 28 | int c; |
| 29 | |
| 30 | while((c=next_char(f)) && c!='\n' && c!=EOF) |
| 31 | ; |
| 32 | } |
| 33 | |
| 34 | int read_config_line(FILE *f, char *line, const char **value, int bufsize) |
| 35 | { |
| 36 | int i = 0, isname = 0; |
| 37 | |
| 38 | *value = NULL; |
| 39 | while(i<bufsize-1) { |
| 40 | int c = next_char(f); |
| 41 | if (!isname && (c=='#' || c==';')) { |
| 42 | skip_line(f); |
| 43 | continue; |
| 44 | } |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 45 | if (!isname && isspace(c)) |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 46 | continue; |
| 47 | |
| 48 | if (c=='=' && !*value) { |
| 49 | line[i] = 0; |
| 50 | *value = &line[i+1]; |
| 51 | } else if (c=='\n' && !isname) { |
| 52 | i = 0; |
| 53 | continue; |
| 54 | } else if (c=='\n' || c==EOF) { |
| 55 | line[i] = 0; |
| 56 | break; |
| 57 | } else { |
| 58 | line[i]=c; |
| 59 | } |
| 60 | isname = 1; |
| 61 | i++; |
| 62 | } |
| 63 | line[i+1] = 0; |
| 64 | return i; |
| 65 | } |
| 66 | |
| 67 | int cgit_read_config(const char *filename, configfn fn) |
| 68 | { |
Lars Hjemli | 5ec6e02 | 2007-05-14 23:40:33 +0200 | [diff] [blame] | 69 | static int nesting; |
| 70 | int len; |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 71 | char line[256]; |
| 72 | const char *value; |
Lars Hjemli | 5ec6e02 | 2007-05-14 23:40:33 +0200 | [diff] [blame] | 73 | FILE *f; |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 74 | |
Lars Hjemli | 47a81c7 | 2007-05-15 23:28:40 +0200 | [diff] [blame] | 75 | /* cancel deeply nested include-commands */ |
| 76 | if (nesting > 8) |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 77 | return -1; |
Lars Hjemli | 5ec6e02 | 2007-05-14 23:40:33 +0200 | [diff] [blame] | 78 | if (!(f = fopen(filename, "r"))) |
| 79 | return -1; |
Lars Hjemli | 47a81c7 | 2007-05-15 23:28:40 +0200 | [diff] [blame] | 80 | nesting++; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 81 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 82 | (*fn)(line, value); |
Lars Hjemli | 47a81c7 | 2007-05-15 23:28:40 +0200 | [diff] [blame] | 83 | nesting--; |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 84 | fclose(f); |
Lars Hjemli | 5ec6e02 | 2007-05-14 23:40:33 +0200 | [diff] [blame] | 85 | return 0; |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Lars Hjemli | 52e605c | 2007-01-04 16:53:03 +0100 | [diff] [blame] | 88 | char *convert_query_hexchar(char *txt) |
| 89 | { |
| 90 | int d1, d2; |
| 91 | if (strlen(txt) < 3) { |
| 92 | *txt = '\0'; |
| 93 | return txt-1; |
| 94 | } |
| 95 | d1 = hextoint(*(txt+1)); |
| 96 | d2 = hextoint(*(txt+2)); |
| 97 | if (d1<0 || d2<0) { |
| 98 | strcpy(txt, txt+3); |
| 99 | return txt-1; |
| 100 | } else { |
| 101 | *txt = d1 * 16 + d2; |
| 102 | strcpy(txt+1, txt+3); |
| 103 | return txt; |
| 104 | } |
| 105 | } |
| 106 | |
Lars Hjemli | 51ada4f | 2006-12-11 16:11:40 +0100 | [diff] [blame] | 107 | int cgit_parse_query(char *txt, configfn fn) |
| 108 | { |
| 109 | char *t, *value = NULL, c; |
| 110 | |
| 111 | if (!txt) |
| 112 | return 0; |
| 113 | |
| 114 | t = txt = xstrdup(txt); |
Lars Hjemli | 47a81c7 | 2007-05-15 23:28:40 +0200 | [diff] [blame] | 115 | |
Lars Hjemli | 51ada4f | 2006-12-11 16:11:40 +0100 | [diff] [blame] | 116 | while((c=*t) != '\0') { |
| 117 | if (c=='=') { |
| 118 | *t = '\0'; |
| 119 | value = t+1; |
Lars Hjemli | 05b1319 | 2006-12-28 02:51:46 +0100 | [diff] [blame] | 120 | } else if (c=='+') { |
| 121 | *t = ' '; |
Lars Hjemli | 52e605c | 2007-01-04 16:53:03 +0100 | [diff] [blame] | 122 | } else if (c=='%') { |
| 123 | t = convert_query_hexchar(t); |
Lars Hjemli | 51ada4f | 2006-12-11 16:11:40 +0100 | [diff] [blame] | 124 | } else if (c=='&') { |
| 125 | *t = '\0'; |
| 126 | (*fn)(txt, value); |
| 127 | txt = t+1; |
| 128 | value = NULL; |
| 129 | } |
| 130 | t++; |
| 131 | } |
| 132 | if (t!=txt) |
| 133 | (*fn)(txt, value); |
| 134 | return 0; |
| 135 | } |
Lars Hjemli | 2101e26 | 2006-12-15 18:17:36 +0100 | [diff] [blame] | 136 | |
Lars Hjemli | 30ccdca | 2007-05-18 03:00:54 +0200 | [diff] [blame] | 137 | /* |
| 138 | * url syntax: [repo ['/' cmd [ '/' path]]] |
| 139 | * repo: any valid repo url, may contain '/' |
| 140 | * cmd: log | commit | diff | tree | view | blob | snapshot |
| 141 | * path: any valid path, may contain '/' |
| 142 | * |
| 143 | */ |
| 144 | void cgit_parse_url(const char *url) |
| 145 | { |
| 146 | char *cmd, *p; |
| 147 | |
| 148 | cgit_repo = NULL; |
| 149 | if (!url || url[0] == '\0') |
| 150 | return; |
| 151 | |
| 152 | cgit_repo = cgit_get_repoinfo(url); |
| 153 | if (cgit_repo) { |
| 154 | cgit_query_repo = cgit_repo->url; |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | cmd = strchr(url, '/'); |
| 159 | while (!cgit_repo && cmd) { |
| 160 | cmd[0] = '\0'; |
| 161 | cgit_repo = cgit_get_repoinfo(url); |
| 162 | if (cgit_repo == NULL) { |
| 163 | cmd[0] = '/'; |
| 164 | cmd = strchr(cmd + 1, '/'); |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | cgit_query_repo = cgit_repo->url; |
| 169 | p = strchr(cmd + 1, '/'); |
| 170 | if (p) { |
| 171 | p[0] = '\0'; |
Lars Hjemli | 3de63b2 | 2007-05-18 13:06:45 +0200 | [diff] [blame] | 172 | if (p[1]) |
Lars Hjemli | 382805e | 2007-06-26 18:04:31 +0200 | [diff] [blame] | 173 | cgit_query_path = trim_end(p + 1, '/'); |
Lars Hjemli | 30ccdca | 2007-05-18 03:00:54 +0200 | [diff] [blame] | 174 | } |
| 175 | cgit_cmd = cgit_get_cmd_index(cmd + 1); |
| 176 | cgit_query_page = xstrdup(cmd + 1); |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | |
Jonathan Bastien-Filiatrault | af08198 | 2007-10-26 18:11:26 -0400 | [diff] [blame^] | 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 | |
Lars Hjemli | 2101e26 | 2006-12-15 18:17:36 +0100 | [diff] [blame] | 237 | char *substr(const char *head, const char *tail) |
| 238 | { |
| 239 | char *buf; |
| 240 | |
| 241 | buf = xmalloc(tail - head + 1); |
| 242 | strncpy(buf, head, tail - head); |
| 243 | buf[tail - head] = '\0'; |
| 244 | return buf; |
| 245 | } |
| 246 | |
| 247 | struct commitinfo *cgit_parse_commit(struct commit *commit) |
| 248 | { |
| 249 | struct commitinfo *ret; |
| 250 | char *p = commit->buffer, *t = commit->buffer; |
| 251 | |
| 252 | ret = xmalloc(sizeof(*ret)); |
| 253 | ret->commit = commit; |
Lars Hjemli | 66091f9 | 2007-01-16 18:41:01 +0100 | [diff] [blame] | 254 | ret->author = NULL; |
| 255 | ret->author_email = NULL; |
| 256 | ret->committer = NULL; |
| 257 | ret->committer_email = NULL; |
| 258 | ret->subject = NULL; |
| 259 | ret->msg = NULL; |
Jonathan Bastien-Filiatrault | 3845e17 | 2007-10-26 18:09:06 -0400 | [diff] [blame] | 260 | ret->msg_encoding = NULL; |
Lars Hjemli | 2101e26 | 2006-12-15 18:17:36 +0100 | [diff] [blame] | 261 | |
Ondrej Jirman | 6130231 | 2007-05-26 03:27:49 +0200 | [diff] [blame] | 262 | if (p == NULL) |
| 263 | return ret; |
| 264 | |
Lars Hjemli | 2101e26 | 2006-12-15 18:17:36 +0100 | [diff] [blame] | 265 | if (strncmp(p, "tree ", 5)) |
| 266 | die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); |
| 267 | else |
| 268 | p += 46; // "tree " + hex[40] + "\n" |
| 269 | |
| 270 | while (!strncmp(p, "parent ", 7)) |
| 271 | p += 48; // "parent " + hex[40] + "\n" |
| 272 | |
| 273 | if (!strncmp(p, "author ", 7)) { |
| 274 | p += 7; |
| 275 | t = strchr(p, '<') - 1; |
| 276 | ret->author = substr(p, t); |
Lars Hjemli | 77078ba | 2006-12-16 14:25:41 +0100 | [diff] [blame] | 277 | p = t; |
| 278 | t = strchr(t, '>') + 1; |
| 279 | ret->author_email = substr(p, t); |
| 280 | ret->author_date = atol(++t); |
| 281 | p = strchr(t, '\n') + 1; |
Lars Hjemli | 2101e26 | 2006-12-15 18:17:36 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | if (!strncmp(p, "committer ", 9)) { |
| 285 | p += 9; |
| 286 | t = strchr(p, '<') - 1; |
| 287 | ret->committer = substr(p, t); |
Lars Hjemli | 77078ba | 2006-12-16 14:25:41 +0100 | [diff] [blame] | 288 | p = t; |
| 289 | t = strchr(t, '>') + 1; |
| 290 | ret->committer_email = substr(p, t); |
| 291 | ret->committer_date = atol(++t); |
| 292 | p = strchr(t, '\n') + 1; |
Lars Hjemli | 2101e26 | 2006-12-15 18:17:36 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Jonathan Bastien-Filiatrault | aa5cc32 | 2007-10-26 18:10:26 -0400 | [diff] [blame] | 295 | if (!strncmp(p, "encoding ", 9)) { |
| 296 | p += 9; |
| 297 | t = strchr(p, '\n') + 1; |
| 298 | ret->msg_encoding = substr(p, t); |
| 299 | p = t; |
| 300 | } else |
| 301 | ret->msg_encoding = xstrdup(PAGE_ENCODING); |
| 302 | |
Lars Hjemli | 13d2b0b | 2007-10-24 21:14:44 +0200 | [diff] [blame] | 303 | while (*p && (*p != '\n')) |
| 304 | p = strchr(p, '\n') + 1; // skip unknown header fields |
| 305 | |
Lars Hjemli | 2101e26 | 2006-12-15 18:17:36 +0100 | [diff] [blame] | 306 | while (*p == '\n') |
| 307 | p = strchr(p, '\n') + 1; |
| 308 | |
| 309 | t = strchr(p, '\n'); |
Ondrej Jirman | 3ce6fc1 | 2007-05-26 02:19:38 +0200 | [diff] [blame] | 310 | if (t) { |
| 311 | if (*t == '\0') |
Lars Hjemli | c61360f | 2007-10-27 13:50:18 +0200 | [diff] [blame] | 312 | ret->subject = "** empty **"; |
Ondrej Jirman | 3ce6fc1 | 2007-05-26 02:19:38 +0200 | [diff] [blame] | 313 | else |
| 314 | ret->subject = substr(p, t); |
Lars Hjemli | 66091f9 | 2007-01-16 18:41:01 +0100 | [diff] [blame] | 315 | p = t + 1; |
Lars Hjemli | 2101e26 | 2006-12-15 18:17:36 +0100 | [diff] [blame] | 316 | |
Lars Hjemli | 66091f9 | 2007-01-16 18:41:01 +0100 | [diff] [blame] | 317 | while (*p == '\n') |
| 318 | p = strchr(p, '\n') + 1; |
Lars Hjemli | c61360f | 2007-10-27 13:50:18 +0200 | [diff] [blame] | 319 | ret->msg = xstrdup(p); |
Ondrej Jirman | 3ce6fc1 | 2007-05-26 02:19:38 +0200 | [diff] [blame] | 320 | } else |
| 321 | ret->subject = substr(p, p+strlen(p)); |
| 322 | |
Lars Hjemli | 2101e26 | 2006-12-15 18:17:36 +0100 | [diff] [blame] | 323 | return ret; |
| 324 | } |
Lars Hjemli | a69061f | 2007-01-17 01:09:51 +0100 | [diff] [blame] | 325 | |
| 326 | |
| 327 | struct taginfo *cgit_parse_tag(struct tag *tag) |
| 328 | { |
| 329 | void *data; |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 330 | enum object_type type; |
Lars Hjemli | a69061f | 2007-01-17 01:09:51 +0100 | [diff] [blame] | 331 | unsigned long size; |
| 332 | char *p, *t; |
| 333 | struct taginfo *ret; |
| 334 | |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 335 | data = read_sha1_file(tag->object.sha1, &type, &size); |
| 336 | if (!data || type != OBJ_TAG) { |
Lars Hjemli | a69061f | 2007-01-17 01:09:51 +0100 | [diff] [blame] | 337 | free(data); |
| 338 | return 0; |
| 339 | } |
Lars Hjemli | 47a81c7 | 2007-05-15 23:28:40 +0200 | [diff] [blame] | 340 | |
Lars Hjemli | a69061f | 2007-01-17 01:09:51 +0100 | [diff] [blame] | 341 | ret = xmalloc(sizeof(*ret)); |
| 342 | ret->tagger = NULL; |
| 343 | ret->tagger_email = NULL; |
| 344 | ret->tagger_date = 0; |
| 345 | ret->msg = NULL; |
| 346 | |
| 347 | p = data; |
| 348 | |
Lars Hjemli | ebd7b0f | 2007-02-03 16:11:41 +0100 | [diff] [blame] | 349 | while (p && *p) { |
Lars Hjemli | a69061f | 2007-01-17 01:09:51 +0100 | [diff] [blame] | 350 | if (*p == '\n') |
| 351 | break; |
| 352 | |
| 353 | if (!strncmp(p, "tagger ", 7)) { |
| 354 | p += 7; |
| 355 | t = strchr(p, '<') - 1; |
| 356 | ret->tagger = substr(p, t); |
| 357 | p = t; |
| 358 | t = strchr(t, '>') + 1; |
| 359 | ret->tagger_email = substr(p, t); |
| 360 | ret->tagger_date = atol(++t); |
| 361 | } |
| 362 | p = strchr(p, '\n') + 1; |
| 363 | } |
| 364 | |
Lars Hjemli | 13d2b0b | 2007-10-24 21:14:44 +0200 | [diff] [blame] | 365 | while (p && *p && (*p != '\n')) |
| 366 | p = strchr(p, '\n') + 1; // skip unknown tag fields |
| 367 | |
Lars Hjemli | a69061f | 2007-01-17 01:09:51 +0100 | [diff] [blame] | 368 | while (p && (*p == '\n')) |
| 369 | p = strchr(p, '\n') + 1; |
Lars Hjemli | ebd7b0f | 2007-02-03 16:11:41 +0100 | [diff] [blame] | 370 | if (p && *p) |
Lars Hjemli | a69061f | 2007-01-17 01:09:51 +0100 | [diff] [blame] | 371 | ret->msg = xstrdup(p); |
| 372 | free(data); |
| 373 | return ret; |
| 374 | } |