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 int next_char(FILE *f)
24 void skip_line(FILE *f)
28 while((c=next_char(f)) && c!='\n' && c!=EOF)
32 int read_config_line(FILE *f, char *line, const char **value, int bufsize)
34 int i = 0, isname = 0;
39 if (!isname && (c=='#' || c==';')) {
43 if (!isname && isspace(c))
46 if (c=='=' && !*value) {
49 } else if (c=='\n' && !isname) {
52 } else if (c=='\n' || c==EOF) {
65 int cgit_read_config(const char *filename, configfn fn)
73 /* cancel deeply nested include-commands */
76 if (!(f = fopen(filename, "r")))
79 while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
86 char *convert_query_hexchar(char *txt)
89 if (strlen(txt) < 3) {
93 d1 = hextoint(*(txt+1));
94 d2 = hextoint(*(txt+2));
100 strcpy(txt+1, txt+3);
105 int cgit_parse_query(char *txt, configfn fn)
107 char *t, *value = NULL, c;
112 t = txt = xstrdup(txt);
114 while((c=*t) != '\0') {
121 t = convert_query_hexchar(t);
136 * url syntax: [repo ['/' cmd [ '/' path]]]
137 * repo: any valid repo url, may contain '/'
138 * cmd: log | commit | diff | tree | view | blob | snapshot
139 * path: any valid path, may contain '/'
142 void cgit_parse_url(const char *url)
147 if (!url || url[0] == '\0')
150 cgit_repo = cgit_get_repoinfo(url);
152 cgit_query_repo = cgit_repo->url;
156 cmd = strchr(url, '/');
157 while (!cgit_repo && cmd) {
159 cgit_repo = cgit_get_repoinfo(url);
160 if (cgit_repo == NULL) {
162 cmd = strchr(cmd + 1, '/');
166 cgit_query_repo = cgit_repo->url;
167 p = strchr(cmd + 1, '/');
170 cgit_query_path = xstrdup(p + 1);
172 cgit_cmd = cgit_get_cmd_index(cmd + 1);
173 cgit_query_page = xstrdup(cmd + 1);
178 char *substr(const char *head, const char *tail)
182 buf = xmalloc(tail - head + 1);
183 strncpy(buf, head, tail - head);
184 buf[tail - head] = '\0';
188 struct commitinfo *cgit_parse_commit(struct commit *commit)
190 struct commitinfo *ret;
191 char *p = commit->buffer, *t = commit->buffer;
193 ret = xmalloc(sizeof(*ret));
194 ret->commit = commit;
196 ret->author_email = NULL;
197 ret->committer = NULL;
198 ret->committer_email = NULL;
202 if (strncmp(p, "tree ", 5))
203 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
205 p += 46; // "tree " + hex[40] + "\n"
207 while (!strncmp(p, "parent ", 7))
208 p += 48; // "parent " + hex[40] + "\n"
210 if (!strncmp(p, "author ", 7)) {
212 t = strchr(p, '<') - 1;
213 ret->author = substr(p, t);
215 t = strchr(t, '>') + 1;
216 ret->author_email = substr(p, t);
217 ret->author_date = atol(++t);
218 p = strchr(t, '\n') + 1;
221 if (!strncmp(p, "committer ", 9)) {
223 t = strchr(p, '<') - 1;
224 ret->committer = substr(p, t);
226 t = strchr(t, '>') + 1;
227 ret->committer_email = substr(p, t);
228 ret->committer_date = atol(++t);
229 p = strchr(t, '\n') + 1;
233 p = strchr(p, '\n') + 1;
237 ret->subject = substr(p, t);
241 p = strchr(p, '\n') + 1;
248 struct taginfo *cgit_parse_tag(struct tag *tag)
251 enum object_type type;
256 data = read_sha1_file(tag->object.sha1, &type, &size);
257 if (!data || type != OBJ_TAG) {
262 ret = xmalloc(sizeof(*ret));
264 ret->tagger_email = NULL;
265 ret->tagger_date = 0;
274 if (!strncmp(p, "tagger ", 7)) {
276 t = strchr(p, '<') - 1;
277 ret->tagger = substr(p, t);
279 t = strchr(t, '>') + 1;
280 ret->tagger_email = substr(p, t);
281 ret->tagger_date = atol(++t);
283 p = strchr(p, '\n') + 1;
286 while (p && (*p == '\n'))
287 p = strchr(p, '\n') + 1;
289 ret->msg = xstrdup(p);