1 /* parsing.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)
21 struct cgit_repo *repo;
24 if (!url || url[0] == '\0')
27 ctx.repo = cgit_get_repoinfo(url);
29 ctx.qry.repo = ctx.repo->url;
37 repo = cgit_get_repoinfo(url);
43 c = strchr(c + 1, '/');
47 ctx.qry.repo = ctx.repo->url;
48 p = strchr(cmd + 1, '/');
52 ctx.qry.path = trim_end(p + 1, '/');
55 ctx.qry.page = xstrdup(cmd + 1);
60 static char *substr(const char *head, const char *tail)
66 buf = xmalloc(tail - head + 1);
67 strncpy(buf, head, tail - head);
68 buf[tail - head] = '\0';
72 static const char *parse_user(const char *t, char **name, char **email, unsigned long *date)
78 if (mode == 1 && *p == '<') {
79 *name = substr(t, p - 1);
82 } else if (mode == 1 && *p == '\n') {
86 } else if (mode == 2 && *p == '>') {
87 *email = substr(t, p + 1);
90 } else if (mode == 2 && *p == '\n') {
91 *email = substr(t, p);
94 } else if (mode == 3 && isdigit(*p)) {
97 } else if (*p == '\n') {
107 #define reencode(a, b, c)
109 static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
116 if (!*txt || !src_enc || !dst_enc)
119 /* no encoding needed if src_enc equals dst_enc */
120 if (!strcasecmp(src_enc, dst_enc))
123 tmp = reencode_string(*txt, dst_enc, src_enc);
132 struct commitinfo *cgit_parse_commit(struct commit *commit)
134 struct commitinfo *ret;
135 const char *p = get_cached_commit_buffer(commit, NULL);
138 ret = xmalloc(sizeof(*ret));
139 ret->commit = commit;
141 ret->author_email = NULL;
142 ret->committer = NULL;
143 ret->committer_email = NULL;
146 ret->msg_encoding = NULL;
151 if (!starts_with(p, "tree "))
152 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
154 p += 46; // "tree " + hex[40] + "\n"
156 while (starts_with(p, "parent "))
157 p += 48; // "parent " + hex[40] + "\n"
159 if (p && starts_with(p, "author ")) {
160 p = parse_user(p + 7, &ret->author, &ret->author_email,
164 if (p && starts_with(p, "committer ")) {
165 p = parse_user(p + 10, &ret->committer, &ret->committer_email,
166 &ret->committer_date);
169 if (p && starts_with(p, "encoding ")) {
173 ret->msg_encoding = substr(p, t + 1);
178 /* if no special encoding is found, assume UTF-8 */
179 if (!ret->msg_encoding)
180 ret->msg_encoding = xstrdup("UTF-8");
182 // skip unknown header fields
183 while (p && *p && (*p != '\n')) {
189 // skip empty lines between headers and message
190 while (p && *p == '\n')
198 ret->subject = substr(p, t);
201 while (p && *p == '\n') {
207 ret->msg = xstrdup(p);
209 ret->subject = xstrdup(p);
211 reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
212 reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
213 reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
214 reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
215 reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
216 reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
222 struct taginfo *cgit_parse_tag(struct tag *tag)
225 enum object_type type;
230 data = read_sha1_file(tag->object.sha1, &type, &size);
231 if (!data || type != OBJ_TAG) {
236 ret = xmalloc(sizeof(*ret));
238 ret->tagger_email = NULL;
239 ret->tagger_date = 0;
248 if (starts_with(p, "tagger ")) {
249 p = parse_user(p + 7, &ret->tagger, &ret->tagger_email,
258 // skip empty lines between headers and message
259 while (p && *p == '\n')
263 ret->msg = xstrdup(p);