]>
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
)
106 if (!txt
|| !*txt
|| !src_enc
|| !dst_enc
)
109 tmp
= reencode_string(*txt
, src_enc
, dst_enc
);
118 struct commitinfo
*cgit_parse_commit(struct commit
*commit
)
120 struct commitinfo
*ret
;
121 char *p
= commit
->buffer
, *t
= commit
->buffer
;
123 ret
= xmalloc(sizeof(*ret
));
124 ret
->commit
= commit
;
126 ret
->author_email
= NULL
;
127 ret
->committer
= NULL
;
128 ret
->committer_email
= NULL
;
131 ret
->msg_encoding
= NULL
;
136 if (strncmp(p
, "tree ", 5))
137 die("Bad commit: %s", sha1_to_hex(commit
->object
.sha1
));
139 p
+= 46; // "tree " + hex[40] + "\n"
141 while (!strncmp(p
, "parent ", 7))
142 p
+= 48; // "parent " + hex[40] + "\n"
144 if (p
&& !strncmp(p
, "author ", 7)) {
145 p
= parse_user(p
+ 7, &ret
->author
, &ret
->author_email
,
149 if (p
&& !strncmp(p
, "committer ", 9)) {
150 p
= parse_user(p
+ 9, &ret
->committer
, &ret
->committer_email
,
151 &ret
->committer_date
);
154 if (p
&& !strncmp(p
, "encoding ", 9)) {
158 ret
->msg_encoding
= substr(p
, t
+ 1);
163 // skip unknown header fields
164 while (p
&& *p
&& (*p
!= '\n')) {
170 // skip empty lines between headers and message
171 while (p
&& *p
== '\n')
179 ret
->subject
= substr(p
, t
);
182 while (p
&& *p
== '\n') {
188 ret
->msg
= xstrdup(p
);
190 ret
->subject
= xstrdup(p
);
192 if (ret
->msg_encoding
) {
193 reencode(&ret
->subject
, PAGE_ENCODING
, ret
->msg_encoding
);
194 reencode(&ret
->msg
, PAGE_ENCODING
, ret
->msg_encoding
);
201 struct taginfo
*cgit_parse_tag(struct tag
*tag
)
204 enum object_type type
;
209 data
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
210 if (!data
|| type
!= OBJ_TAG
) {
215 ret
= xmalloc(sizeof(*ret
));
217 ret
->tagger_email
= NULL
;
218 ret
->tagger_date
= 0;
227 if (!strncmp(p
, "tagger ", 7)) {
228 p
= parse_user(p
+ 7, &ret
->tagger
, &ret
->tagger_email
,
237 // skip empty lines between headers and message
238 while (p
&& *p
== '\n')
242 ret
->msg
= xstrdup(p
);