1 /* config.c: parsing of config files
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 * url syntax: [repo ['/' cmd [ '/' path]]]
13 * repo: any valid repo url, may contain '/'
14 * cmd: log | commit | diff | tree | view | blob | snapshot
15 * path: any valid path, may contain '/'
18 void cgit_parse_url(const char *url)
23 if (!url || url[0] == '\0')
26 ctx.repo = cgit_get_repoinfo(url);
28 ctx.qry.repo = ctx.repo->url;
32 cmd = strchr(url, '/');
33 while (!ctx.repo && cmd) {
35 ctx.repo = cgit_get_repoinfo(url);
36 if (ctx.repo == NULL) {
38 cmd = strchr(cmd + 1, '/');
42 ctx.qry.repo = ctx.repo->url;
43 p = strchr(cmd + 1, '/');
47 ctx.qry.path = trim_end(p + 1, '/');
50 ctx.qry.page = xstrdup(cmd + 1);
55 static char *substr(const char *head, const char *tail)
61 buf = xmalloc(tail - head + 1);
62 strncpy(buf, head, tail - head);
63 buf[tail - head] = '\0';
67 static char *parse_user(char *t, char **name, char **email, unsigned long *date)
73 if (mode == 1 && *p == '<') {
74 *name = substr(t, p - 1);
77 } else if (mode == 1 && *p == '\n') {
81 } else if (mode == 2 && *p == '>') {
82 *email = substr(t, p + 1);
85 } else if (mode == 2 && *p == '\n') {
86 *email = substr(t, p);
89 } else if (mode == 3 && isdigit(*p)) {
92 } else if (*p == '\n') {
102 #define reencode(a, b, c)
104 static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
111 if (!*txt || !src_enc || !dst_enc)
114 /* no encoding needed if src_enc equals dst_enc */
115 if (!strcasecmp(src_enc, dst_enc))
118 tmp = reencode_string(*txt, dst_enc, src_enc);
127 struct commitinfo *cgit_parse_commit(struct commit *commit)
129 struct commitinfo *ret;
130 char *p = commit->buffer, *t;
132 ret = xmalloc(sizeof(*ret));
133 ret->commit = commit;
135 ret->author_email = NULL;
136 ret->committer = NULL;
137 ret->committer_email = NULL;
140 ret->msg_encoding = NULL;
145 if (strncmp(p, "tree ", 5))
146 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
148 p += 46; // "tree " + hex[40] + "\n"
150 while (!strncmp(p, "parent ", 7))
151 p += 48; // "parent " + hex[40] + "\n"
153 if (p && !strncmp(p, "author ", 7)) {
154 p = parse_user(p + 7, &ret->author, &ret->author_email,
158 if (p && !strncmp(p, "committer ", 9)) {
159 p = parse_user(p + 9, &ret->committer, &ret->committer_email,
160 &ret->committer_date);
163 if (p && !strncmp(p, "encoding ", 9)) {
167 ret->msg_encoding = substr(p, t + 1);
172 /* if no special encoding is found, assume UTF-8 */
173 if (!ret->msg_encoding)
174 ret->msg_encoding = xstrdup("UTF-8");
176 // skip unknown header fields
177 while (p && *p && (*p != '\n')) {
183 // skip empty lines between headers and message
184 while (p && *p == '\n')
192 ret->subject = substr(p, t);
195 while (p && *p == '\n') {
201 ret->msg = xstrdup(p);
203 ret->subject = xstrdup(p);
205 reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
206 reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
207 reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
208 reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
209 reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
210 reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
216 struct taginfo *cgit_parse_tag(struct tag *tag)
219 enum object_type type;
224 data = read_sha1_file(tag->object.sha1, &type, &size);
225 if (!data || type != OBJ_TAG) {
230 ret = xmalloc(sizeof(*ret));
232 ret->tagger_email = NULL;
233 ret->tagger_date = 0;
242 if (!strncmp(p, "tagger ", 7)) {
243 p = parse_user(p + 7, &ret->tagger, &ret->tagger_email,
252 // skip empty lines between headers and message
253 while (p && *p == '\n')
257 ret->msg = xstrdup(p);