]> gitweb.ps.run Git - ps-cgit/blob - parsing.c
Fix cgit_parse_url when a repo url is contained in another repo url
[ps-cgit] / parsing.c
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  */
18 void cgit_parse_url(const char *url)
19 {
20         char *c, *cmd, *p;
21         struct cgit_repo *repo;
22
23         ctx.repo = NULL;
24         if (!url || url[0] == '\0')
25                 return;
26
27         ctx.repo = cgit_get_repoinfo(url);
28         if (ctx.repo) {
29                 ctx.qry.repo = ctx.repo->url;
30                 return;
31         }
32
33         cmd = NULL;
34         c = strchr(url, '/');
35         while (c) {
36                 c[0] = '\0';
37                 repo = cgit_get_repoinfo(url);
38                 if (repo) {
39                         ctx.repo = repo;
40                         cmd = c;
41                 }
42                 c[0] = '/';
43                 c = strchr(c + 1, '/');
44         }
45
46         if (ctx.repo) {
47                 ctx.qry.repo = ctx.repo->url;
48                 p = strchr(cmd + 1, '/');
49                 if (p) {
50                         p[0] = '\0';
51                         if (p[1])
52                                 ctx.qry.path = trim_end(p + 1, '/');
53                 }
54                 if (cmd[1])
55                         ctx.qry.page = xstrdup(cmd + 1);
56                 return;
57         }
58 }
59
60 static 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
72 static char *parse_user(char *t, char **name, char **email, unsigned long *date)
73 {
74         char *p = t;
75         int mode = 1;
76
77         while (p && *p) {
78                 if (mode == 1 && *p == '<') {
79                         *name = substr(t, p - 1);
80                         t = p;
81                         mode++;
82                 } else if (mode == 1 && *p == '\n') {
83                         *name = substr(t, p);
84                         p++;
85                         break;
86                 } else if (mode == 2 && *p == '>') {
87                         *email = substr(t, p + 1);
88                         t = p;
89                         mode++;
90                 } else if (mode == 2 && *p == '\n') {
91                         *email = substr(t, p);
92                         p++;
93                         break;
94                 } else if (mode == 3 && isdigit(*p)) {
95                         *date = atol(p);
96                         mode++;
97                 } else if (*p == '\n') {
98                         p++;
99                         break;
100                 }
101                 p++;
102         }
103         return p;
104 }
105
106 #ifdef NO_ICONV
107 #define reencode(a, b, c)
108 #else
109 static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
110 {
111         char *tmp;
112
113         if (!txt)
114                 return NULL;
115
116         if (!*txt || !src_enc || !dst_enc)
117                 return *txt;
118
119         /* no encoding needed if src_enc equals dst_enc */
120         if (!strcasecmp(src_enc, dst_enc))
121                 return *txt;
122
123         tmp = reencode_string(*txt, dst_enc, src_enc);
124         if (tmp) {
125                 free(*txt);
126                 *txt = tmp;
127         }
128         return *txt;
129 }
130 #endif
131
132 struct commitinfo *cgit_parse_commit(struct commit *commit)
133 {
134         struct commitinfo *ret;
135         char *p = commit->buffer, *t;
136
137         ret = xmalloc(sizeof(*ret));
138         ret->commit = commit;
139         ret->author = NULL;
140         ret->author_email = NULL;
141         ret->committer = NULL;
142         ret->committer_email = NULL;
143         ret->subject = NULL;
144         ret->msg = NULL;
145         ret->msg_encoding = NULL;
146
147         if (p == NULL)
148                 return ret;
149
150         if (prefixcmp(p, "tree "))
151                 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
152         else
153                 p += 46; // "tree " + hex[40] + "\n"
154
155         while (!prefixcmp(p, "parent "))
156                 p += 48; // "parent " + hex[40] + "\n"
157
158         if (p && !prefixcmp(p, "author ")) {
159                 p = parse_user(p + 7, &ret->author, &ret->author_email,
160                         &ret->author_date);
161         }
162
163         if (p && !prefixcmp(p, "committer ")) {
164                 p = parse_user(p + 10, &ret->committer, &ret->committer_email,
165                         &ret->committer_date);
166         }
167
168         if (p && !prefixcmp(p, "encoding ")) {
169                 p += 9;
170                 t = strchr(p, '\n');
171                 if (t) {
172                         ret->msg_encoding = substr(p, t + 1);
173                         p = t + 1;
174                 }
175         }
176
177         /* if no special encoding is found, assume UTF-8 */
178         if (!ret->msg_encoding)
179                 ret->msg_encoding = xstrdup("UTF-8");
180
181         // skip unknown header fields
182         while (p && *p && (*p != '\n')) {
183                 p = strchr(p, '\n');
184                 if (p)
185                         p++;
186         }
187
188         // skip empty lines between headers and message
189         while (p && *p == '\n')
190                 p++;
191
192         if (!p)
193                 return ret;
194
195         t = strchr(p, '\n');
196         if (t) {
197                 ret->subject = substr(p, t);
198                 p = t + 1;
199
200                 while (p && *p == '\n') {
201                         p = strchr(p, '\n');
202                         if (p)
203                                 p++;
204                 }
205                 if (p)
206                         ret->msg = xstrdup(p);
207         } else
208                 ret->subject = xstrdup(p);
209
210         reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
211         reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
212         reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
213         reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
214         reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
215         reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
216
217         return ret;
218 }
219
220
221 struct taginfo *cgit_parse_tag(struct tag *tag)
222 {
223         void *data;
224         enum object_type type;
225         unsigned long size;
226         char *p;
227         struct taginfo *ret;
228
229         data = read_sha1_file(tag->object.sha1, &type, &size);
230         if (!data || type != OBJ_TAG) {
231                 free(data);
232                 return 0;
233         }
234
235         ret = xmalloc(sizeof(*ret));
236         ret->tagger = NULL;
237         ret->tagger_email = NULL;
238         ret->tagger_date = 0;
239         ret->msg = NULL;
240
241         p = data;
242
243         while (p && *p) {
244                 if (*p == '\n')
245                         break;
246
247                 if (!prefixcmp(p, "tagger ")) {
248                         p = parse_user(p + 7, &ret->tagger, &ret->tagger_email,
249                                 &ret->tagger_date);
250                 } else {
251                         p = strchr(p, '\n');
252                         if (p)
253                                 p++;
254                 }
255         }
256
257         // skip empty lines between headers and message
258         while (p && *p == '\n')
259                 p++;
260
261         if (p && *p)
262                 ret->msg = xstrdup(p);
263         free(data);
264         return ret;
265 }