parsing.c (view raw)
1/* parsing.c: parsing of config files
2 *
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
10
11/*
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 '/'
16 *
17 */
18void cgit_parse_url(const char *url)
19{
20 char *c, *cmd, *p;
21 struct cgit_repo *repo;
22
23 ctx.repo = NULL;
24 ctx.qry.page = NULL;
25 if (!url || url[0] == '\0')
26 return;
27
28 ctx.repo = cgit_get_repoinfo(url);
29 if (ctx.repo) {
30 ctx.qry.repo = ctx.repo->url;
31 return;
32 }
33
34 cmd = NULL;
35 c = strchr(url, '/');
36 while (c) {
37 c[0] = '\0';
38 repo = cgit_get_repoinfo(url);
39 if (repo) {
40 ctx.repo = repo;
41 cmd = c;
42 }
43 c[0] = '/';
44 c = strchr(c + 1, '/');
45 }
46
47 if (ctx.repo) {
48 ctx.qry.repo = ctx.repo->url;
49 p = strchr(cmd + 1, '/');
50 if (p) {
51 p[0] = '\0';
52 if (p[1])
53 ctx.qry.path = trim_end(p + 1, '/');
54 }
55 if (cmd[1])
56 ctx.qry.page = xstrdup(cmd + 1);
57 }
58}
59
60static char *substr(const char *head, const char *tail)
61{
62 char *buf;
63
64 if (tail < head)
65 return xstrdup("");
66 buf = xmalloc(tail - head + 1);
67 strncpy(buf, head, tail - head);
68 buf[tail - head] = '\0';
69 return buf;
70}
71
72static void parse_user(const char *t, char **name, char **email, unsigned long *date, int *tz)
73{
74 struct ident_split ident;
75 unsigned email_len;
76
77 if (!split_ident_line(&ident, t, strchrnul(t, '\n') - t)) {
78 *name = substr(ident.name_begin, ident.name_end);
79
80 email_len = ident.mail_end - ident.mail_begin;
81 *email = xmalloc(strlen("<") + email_len + strlen(">") + 1);
82 sprintf(*email, "<%.*s>", email_len, ident.mail_begin);
83
84 if (ident.date_begin)
85 *date = strtoul(ident.date_begin, NULL, 10);
86 if (ident.tz_begin)
87 *tz = atoi(ident.tz_begin);
88 }
89}
90
91#ifdef NO_ICONV
92#define reencode(a, b, c)
93#else
94static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
95{
96 char *tmp;
97
98 if (!txt)
99 return NULL;
100
101 if (!*txt || !src_enc || !dst_enc)
102 return *txt;
103
104 /* no encoding needed if src_enc equals dst_enc */
105 if (!strcasecmp(src_enc, dst_enc))
106 return *txt;
107
108 tmp = reencode_string(*txt, dst_enc, src_enc);
109 if (tmp) {
110 free(*txt);
111 *txt = tmp;
112 }
113 return *txt;
114}
115#endif
116
117static const char *next_header_line(const char *p)
118{
119 p = strchr(p, '\n');
120 if (!p)
121 return NULL;
122 return p + 1;
123}
124
125static int end_of_header(const char *p)
126{
127 return !p || (*p == '\n');
128}
129
130struct commitinfo *cgit_parse_commit(struct commit *commit)
131{
132 const int sha1hex_len = 40;
133 struct commitinfo *ret;
134 const char *p = get_cached_commit_buffer(commit, NULL);
135 const char *t;
136
137 ret = xcalloc(1, sizeof(struct commitinfo));
138 ret->commit = commit;
139
140 if (!p)
141 return ret;
142
143 if (!skip_prefix(p, "tree ", &p))
144 die("Bad commit: %s", oid_to_hex(&commit->object.oid));
145 p += sha1hex_len + 1;
146
147 while (skip_prefix(p, "parent ", &p))
148 p += sha1hex_len + 1;
149
150 if (p && skip_prefix(p, "author ", &p)) {
151 parse_user(p, &ret->author, &ret->author_email,
152 &ret->author_date, &ret->author_tz);
153 p = next_header_line(p);
154 }
155
156 if (p && skip_prefix(p, "committer ", &p)) {
157 parse_user(p, &ret->committer, &ret->committer_email,
158 &ret->committer_date, &ret->committer_tz);
159 p = next_header_line(p);
160 }
161
162 if (p && skip_prefix(p, "encoding ", &p)) {
163 t = strchr(p, '\n');
164 if (t) {
165 ret->msg_encoding = substr(p, t + 1);
166 p = t + 1;
167 }
168 }
169
170 if (!ret->msg_encoding)
171 ret->msg_encoding = xstrdup("UTF-8");
172
173 while (!end_of_header(p))
174 p = next_header_line(p);
175 while (p && *p == '\n')
176 p++;
177 if (!p)
178 return ret;
179
180 t = strchrnul(p, '\n');
181 ret->subject = substr(p, t);
182 while (*t == '\n')
183 t++;
184 ret->msg = xstrdup(t);
185
186 reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
187 reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
188 reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
189 reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
190 reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
191 reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
192
193 return ret;
194}
195
196struct taginfo *cgit_parse_tag(struct tag *tag)
197{
198 void *data;
199 enum object_type type;
200 unsigned long size;
201 const char *p;
202 struct taginfo *ret = NULL;
203
204 data = read_sha1_file(tag->object.oid.hash, &type, &size);
205 if (!data || type != OBJ_TAG)
206 goto cleanup;
207
208 ret = xcalloc(1, sizeof(struct taginfo));
209
210 for (p = data; !end_of_header(p); p = next_header_line(p)) {
211 if (skip_prefix(p, "tagger ", &p)) {
212 parse_user(p, &ret->tagger, &ret->tagger_email,
213 &ret->tagger_date, &ret->tagger_tz);
214 }
215 }
216
217 while (p && *p == '\n')
218 p++;
219
220 if (p && *p)
221 ret->msg = xstrdup(p);
222
223cleanup:
224 free(data);
225 return ret;
226}