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)
11 char *convert_query_hexchar(char *txt)
14 if (strlen(txt) < 3) {
18 d1 = hextoint(*(txt+1));
19 d2 = hextoint(*(txt+2));
30 int cgit_parse_query(char *txt, configfn fn)
32 char *t, *value = NULL, c;
37 t = txt = xstrdup(txt);
39 while((c=*t) != '\0') {
46 t = convert_query_hexchar(t);
61 * url syntax: [repo ['/' cmd [ '/' path]]]
62 * repo: any valid repo url, may contain '/'
63 * cmd: log | commit | diff | tree | view | blob | snapshot
64 * path: any valid path, may contain '/'
67 void cgit_parse_url(const char *url)
72 if (!url || url[0] == '\0')
75 ctx.repo = cgit_get_repoinfo(url);
77 ctx.qry.repo = ctx.repo->url;
81 cmd = strchr(url, '/');
82 while (!ctx.repo && cmd) {
84 ctx.repo = cgit_get_repoinfo(url);
85 if (ctx.repo == NULL) {
87 cmd = strchr(cmd + 1, '/');
91 ctx.qry.repo = ctx.repo->url;
92 p = strchr(cmd + 1, '/');
96 ctx.qry.path = trim_end(p + 1, '/');
99 ctx.qry.page = xstrdup(cmd + 1);
104 char *substr(const char *head, const char *tail)
108 buf = xmalloc(tail - head + 1);
109 strncpy(buf, head, tail - head);
110 buf[tail - head] = '\0';
114 struct commitinfo *cgit_parse_commit(struct commit *commit)
116 struct commitinfo *ret;
117 char *p = commit->buffer, *t = commit->buffer;
119 ret = xmalloc(sizeof(*ret));
120 ret->commit = commit;
122 ret->author_email = NULL;
123 ret->committer = NULL;
124 ret->committer_email = NULL;
127 ret->msg_encoding = NULL;
132 if (strncmp(p, "tree ", 5))
133 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
135 p += 46; // "tree " + hex[40] + "\n"
137 while (!strncmp(p, "parent ", 7))
138 p += 48; // "parent " + hex[40] + "\n"
140 if (!strncmp(p, "author ", 7)) {
142 t = strchr(p, '<') - 1;
143 ret->author = substr(p, t);
145 t = strchr(t, '>') + 1;
146 ret->author_email = substr(p, t);
147 ret->author_date = atol(t+1);
148 p = strchr(t, '\n') + 1;
151 if (!strncmp(p, "committer ", 9)) {
153 t = strchr(p, '<') - 1;
154 ret->committer = substr(p, t);
156 t = strchr(t, '>') + 1;
157 ret->committer_email = substr(p, t);
158 ret->committer_date = atol(t+1);
159 p = strchr(t, '\n') + 1;
162 if (!strncmp(p, "encoding ", 9)) {
164 t = strchr(p, '\n') + 1;
165 ret->msg_encoding = substr(p, t);
168 ret->msg_encoding = xstrdup(PAGE_ENCODING);
170 while (*p && (*p != '\n'))
171 p = strchr(p, '\n') + 1; // skip unknown header fields
174 p = strchr(p, '\n') + 1;
179 ret->subject = "** empty **";
181 ret->subject = substr(p, t);
185 p = strchr(p, '\n') + 1;
186 ret->msg = xstrdup(p);
188 ret->subject = substr(p, p+strlen(p));
190 if(strcmp(ret->msg_encoding, PAGE_ENCODING)) {
191 t = reencode_string(ret->subject, PAGE_ENCODING,
198 t = reencode_string(ret->msg, PAGE_ENCODING,
210 struct taginfo *cgit_parse_tag(struct tag *tag)
213 enum object_type type;
218 data = read_sha1_file(tag->object.sha1, &type, &size);
219 if (!data || type != OBJ_TAG) {
224 ret = xmalloc(sizeof(*ret));
226 ret->tagger_email = NULL;
227 ret->tagger_date = 0;
236 if (!strncmp(p, "tagger ", 7)) {
238 t = strchr(p, '<') - 1;
239 ret->tagger = substr(p, t);
241 t = strchr(t, '>') + 1;
242 ret->tagger_email = substr(p, t);
243 ret->tagger_date = atol(t+1);
245 p = strchr(p, '\n') + 1;
248 while (p && *p && (*p != '\n'))
249 p = strchr(p, '\n') + 1; // skip unknown tag fields
251 while (p && (*p == '\n'))
252 p = strchr(p, '\n') + 1;
254 ret->msg = xstrdup(p);