]>
git.cameronkatri.com Git - cgit.git/blob - parsing.c
1 /* config.c: parsing of config files
3 * Copyright (C) 2006 Lars Hjemli
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 char *substr(const char *head
, const char *tail
)
59 buf
= xmalloc(tail
- head
+ 1);
60 strncpy(buf
, head
, tail
- head
);
61 buf
[tail
- head
] = '\0';
65 char *parse_user(char *t
, char **name
, char **email
, unsigned long *date
)
71 if (mode
== 1 && *p
== '<') {
72 *name
= substr(t
, p
- 1);
75 } else if (mode
== 1 && *p
== '\n') {
79 } else if (mode
== 2 && *p
== '>') {
80 *email
= substr(t
, p
+ 1);
83 } else if (mode
== 2 && *p
== '\n') {
84 *email
= substr(t
, p
);
87 } else if (mode
== 3 && isdigit(*p
)) {
90 } else if (*p
== '\n') {
100 #define reencode(a, b, c)
102 const char *reencode(char **txt
, const char *src_enc
, const char *dst_enc
)
109 if (!*txt
|| !src_enc
|| !dst_enc
)
112 /* no encoding needed if src_enc equals dst_enc */
113 if(!strcasecmp(src_enc
, dst_enc
))
116 tmp
= reencode_string(*txt
, dst_enc
, src_enc
);
125 struct commitinfo
*cgit_parse_commit(struct commit
*commit
)
127 struct commitinfo
*ret
;
128 char *p
= commit
->buffer
, *t
;
130 ret
= xmalloc(sizeof(*ret
));
131 ret
->commit
= commit
;
133 ret
->author_email
= NULL
;
134 ret
->committer
= NULL
;
135 ret
->committer_email
= NULL
;
138 ret
->msg_encoding
= NULL
;
143 if (strncmp(p
, "tree ", 5))
144 die("Bad commit: %s", sha1_to_hex(commit
->object
.sha1
));
146 p
+= 46; // "tree " + hex[40] + "\n"
148 while (!strncmp(p
, "parent ", 7))
149 p
+= 48; // "parent " + hex[40] + "\n"
151 if (p
&& !strncmp(p
, "author ", 7)) {
152 p
= parse_user(p
+ 7, &ret
->author
, &ret
->author_email
,
156 if (p
&& !strncmp(p
, "committer ", 9)) {
157 p
= parse_user(p
+ 9, &ret
->committer
, &ret
->committer_email
,
158 &ret
->committer_date
);
161 if (p
&& !strncmp(p
, "encoding ", 9)) {
165 ret
->msg_encoding
= substr(p
, t
+ 1);
170 /* if no special encoding is found, assume UTF-8 */
171 if(!ret
->msg_encoding
)
172 ret
->msg_encoding
= xstrdup("UTF-8");
174 // skip unknown header fields
175 while (p
&& *p
&& (*p
!= '\n')) {
181 // skip empty lines between headers and message
182 while (p
&& *p
== '\n')
190 ret
->subject
= substr(p
, t
);
193 while (p
&& *p
== '\n') {
199 ret
->msg
= xstrdup(p
);
201 ret
->subject
= xstrdup(p
);
203 reencode(&ret
->author
, ret
->msg_encoding
, PAGE_ENCODING
);
204 reencode(&ret
->author_email
, ret
->msg_encoding
, PAGE_ENCODING
);
205 reencode(&ret
->committer
, ret
->msg_encoding
, PAGE_ENCODING
);
206 reencode(&ret
->committer_email
, ret
->msg_encoding
, PAGE_ENCODING
);
207 reencode(&ret
->subject
, ret
->msg_encoding
, PAGE_ENCODING
);
208 reencode(&ret
->msg
, ret
->msg_encoding
, PAGE_ENCODING
);
214 struct taginfo
*cgit_parse_tag(struct tag
*tag
)
217 enum object_type type
;
222 data
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
223 if (!data
|| type
!= OBJ_TAG
) {
228 ret
= xmalloc(sizeof(*ret
));
230 ret
->tagger_email
= NULL
;
231 ret
->tagger_date
= 0;
240 if (!strncmp(p
, "tagger ", 7)) {
241 p
= parse_user(p
+ 7, &ret
->tagger
, &ret
->tagger_email
,
250 // skip empty lines between headers and message
251 while (p
&& *p
== '\n')
255 ret
->msg
= xstrdup(p
);